Shell script invoked by AppleScript not working

I was trying to convert Chinese characters to Pinyin with a package developed by JavaScript.

And this is the solution in the yaml block:

# popclip Pinyin
name: Pinyin
title: Chinese Pinyin
icon: square 拼
applescript: do shell script "/opt/homebrew/bin/pinyin -s NORMAL -S -g {popclip text}"
after: paste-result

I can confirm that the shell command pinyin -s NORMAL -S -g [some Chinese characters] worked well in the Terminal. However, after the installation of the yaml block above, the extension did not work.

So I also created an extension with a shell file, an icon and Config.plist. And the shell script is:

#!/bin/zsh
/opt/homebrew/bin/pinyin -s NORMAL -S -g $POPCLIP_TEXT

Fortunately, It worked as expected.

So my question is why the extension invoked by AppleScript did not work, while the shell script worked? Thanks in advance!

Hey, I’m just looking into it, can you tell me what homebrew command I can use to install that tool. Is it libpinyin?:

I tried:

> brew install pinyin
Warning: No available formula with the name "pinyin".
==> **Searching for similarly named formulae...**
This similarly named formula was found:
libpinyin
To install it, run:
brew install libpinyin

(P.s. do you know about Pinyin — PopClip Extensions)

It can be installed via NPM:

npm install pinyin@alpha --save

or install it globally:

npm install pinyin@alpha --save -g

Yes, I know it. But I would like to customize the output. For example, group the pinyin based on phrases, disable diacritics etc.

In addition, with the library of Pinyin, I don’t have to maintain a large JavaScript file containing the pinyin data by myself.

1 Like

I wonder is there any updates of this issue?

The reason is that there is no return value from the AppleScript. You need to add return, like this:

applescript: return do shell script "..."

By the way, if you option(⌥)-click the popclip menu bar, you’ll see a debug window that gives some clues as to the problem when things go wrong (I would like to improve the debug output but it is a start).

After adding the return, it didn’t still work. I followed your tip to debug it, but can’t figure out what’s wrong with it.

Ah, I was testing on my latest development version of PopClip, where it works (I’ve done some fixes to the AppleScript code).

I’ll release a beta version soon — I’ll let you know.

The beta I mentioned is available now: PopClip Downloads

1 Like

Unfortunately, after installing the beta version and adding the return, it didn’t work. I guess it’s not the problem of AppleScript.

Shell script not working

Could you option-click the PopClip menu bar for me and see the log window? When the extension fails, what is the error message it shows?

This is my extension:

# popclip Pinyin
name: Pinyin
title: Chinese Pinyin
icon: square 拼
applescript: return do shell script "/opt/homebrew/bin/pinyin -s NORMAL -S -g {popclip text}"
after: paste-result

And the debug info:

Thanks. Strange, it works on mine. The error seems to be coming from the shell script running within the applescript. It seems an env command is not finding your node. Something different between our machines about the way the user path is set up in environment variables, perhaps.

Wait, it’s stopped working again for me. OK, I’ll have to leave this for now but I’ll see if I can get to the bottom of it.

I think I will prefer though to make shell script snippets possible, without having to embed them in AppleScript in the first place.

Yes, currently I made a shell script snippet as an alternative.

Thanks for your reply!

Hey @tomben, I just wanted to let you know about an improvement in PopClip 2022.12. You can now put a shell script directly in a snippet without having to wrap it in an AppleScript anymore.

Like this:

# popclip Pinyin
name: Pinyin
title: Chinese Pinyin
icon: square 拼
after: paste-result
shell script: /opt/homebrew/bin/pinyin -s NORMAL -S -g "$POPCLIP_TEXT"
interpreter: zsh # note that an interpreter must be specified

Also, there is a new snippet header concept. So you can do it this way now:

# #popclip Pinyin
# { name: Pinyin, icon: square 拼, after: paste-result, interpreter: sh }
/opt/homebrew/bin/pinyin -s NORMAL -S -g $POPCLIP_TEXT
2 Likes

This is so great. Thanks for your work!

1 Like