Jump to content

AppleScript does not work?

Recommended Posts

Hi guys, I wrote a little AppleScript that copies and pastes a text selection to the Alfred bar with a certain prefix: see below.


The thing is, when I select text, and run the .scpt file from the Alfred bar (i.e. I look up the file and press enter) it works fine, but when I try to link this  script to an Alfred keyword or hotkey, it stops working. I tried these three methods:


  • I simply copied the content of the .scpt file into an Alfred workflow (actions --> RunNSAppleScript). Doesn't work.
  • I wrote a bash script calling the .scpt file and attached it to the action  "run script". Doesn't work.
  • I used the action "open file" and attached the .scpt file to it. Doesn't work.


It's not that these three methods don't do anything. In fact weird things happen. The text selection expands to adjacent words for example. Or I hear the "beep" of the mac "alert sound". I also sometimes seems as if the delay of 0.5 does not work.


Its' not such a big deal. I can run the script by simply launching it by searching the file in the Alfred bar, but I'm still generally curious what is going on here. Any ideas?



tell application "System Events" to keystroke "c" using command down

tell application "System Events" to keystroke space using command down

delay 0.5

tell application "System Events" to keystroke "n"

tell application "System Events" to keystroke space

tell application "System Events" to keystroke "v" using command down


Share this post

Link to post

@Misha Could you clarify what you're actually trying to achieve? Given the range of objects in Alfred, it's likely that there's a better way to achieve what you want. Are you trying to open Alfred with the macOS selection, with it prefixed with the keyword "n"? If so, that's quite easy and would be much more reliable... (You'll just need to also set a hotkey, which I didn't do in this example)


Screen Shot 2017-07-04 at 17.20.11.png


Beyond that, Alfred has a built-in Dispatch Key Combo output object, a Delay utility object, etc, so you might be able to do what you need without AppleScript keystrokes. :)



Share this post

Link to post

Just to second what Vero said, having done a lot of UI scripting in AppleScript, I would only use it as an absolute last resort...

Share this post

Link to post

@Vero, just wondering, is it maybe also possible to activate a workflow with the "selection in macOS" as argument by using a keyword as a trigger (rather than a hotkey)?

Edited by Misha

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...