Jump to content
kodiak

[SOLVED] Best Method for Calling External AppleScripts

Recommended Posts

Hi Everyone,

 

I'm building a small workflow that should run a .scpt file with keyword. I would really like to leave the file as is so I'll be able to edit it using Apple's Script Editor or Script Debugger, as they offer lots of benefits directly aimed for writing AppleScripts. Here are my considerations:

 

1. Using the Run Script action with the External Script option won't work.

2. Running the script within Alfred or embedding it in a shell script (and either running it in Alfred or as an external script), and I won't be able to use Script Editor or Script Debugger.

3. Choosing to run AppleScripts in Alfred's preferences (instead of editing them), and then setting the workflow to simply open a file means relying on intricate paths. Ideally, the AppleScripts would be called from the workflow's folder with as minimal paths as possible (./).

 

What should I do?

 

Thanks!

Share this post


Link to post

  1. Put your Script.scpt (or whatever) inside the Workflow’s directory.
  2. Make a Run Script with /bin/bash as the Language.
  3. Make it osascript ./Script.scpt.

Share this post


Link to post

Brilliant, thank you @vitor!

 

And while we're at it, how do I pass an input from the user, as an argument, beginning in Alfred's bar, through that shell script and all the way to the AppleScript?

 

Thanks again.

Share this post


Link to post

Thanks!

 

And in the script itself, should I do something like the following?

on run(q)
end run

Where q is the argument.

Share this post


Link to post
5 hours ago, kodiak said:

Thanks!

 

And in the script itself, should I do something like the following?


on run(q)
end run

Where q is the argument.

 

Yes.

Share this post


Link to post
Posted (edited)

Thank you very!

 

I have been implementing your suggestion with great success up until this script:

 

set originalMouseLocation to AST mouse point location
tell application "System Events" to tell process "Dropbox"
	set dropboxLocation to position of menu bar item 1 of menu bar 2
end tell
set dropboxLocationX to item 1 of dropboxLocation
set dropboxLocationY to item 2 of dropboxLocation
AST click at {dropboxLocationX, dropboxLocationY} holding modifier key ast option key
AST set mouse point location originalMouseLocation

Whether run within Alfred or by the way of your suggestion, the script behaves weirdly:

 

1. The last line is not executed (returning the pointer to its original location).

2. For some odd reason, running this script triggers a Dropbox synchronization for .scpt file, as if something has changed within the script.

 

Running the script through Script Debugger or Script Editor works perfectly. 

Help would be very appreciated!

 

Thanks!

Edited by kodiak

Share this post


Link to post

When asking for help with a Workflow, please upload it somewhere as we can’t properly help you without access to it. There are multiple places where your code or Workflow setup may be going wrong.

 

Also GUI scripting is notoriously finicky, and you shouldn’t do more than what you absolutely need. There are ways in AppleScript to activate menus without moving the mouse to click them (not sure they support alternative menus like in this case, though).

Share this post


Link to post
33 minutes ago, kodiak said:

For some odd reason, running this script triggers a Dropbox synchronization for .scpt file, as if something has changed within the script.

 

I've noticed this. macOS appears to re-compile the script files (or otherwise do something to them) when it runs them.

Share this post


Link to post
Posted (edited)
23 hours ago, vitor said:

When asking for help with a Workflow, please upload it somewhere as we can’t properly help you without access to it. There are multiple places where your code or Workflow setup may be going wrong.

 

Also GUI scripting is notoriously finicky, and you shouldn’t do more than what you absolutely need. There are ways in AppleScript to activate menus without moving the mouse to click them (not sure they support alternative menus like in this case, though).

 

1. You're correct, my bad. Here's a link to the workflow (sorry for the mess, it's a work in progress).

https://www.dropbox.com/s/x0d4fpnia327oot/Menu Bar Icons.alfredworkflow?dl=0

 

2. I know, I don't like to rely on GUI scripting and even more so on GUI scripting that revolves moving the pointer around, but in cases such as this (where I need to option-click something), there're no other options.

 

23 hours ago, deanishe said:

 

I've noticed this. macOS appears to re-compile the script files (or otherwise do something to them) when it runs them.

 

3. I see, if so, then why it doesn't happen in all scripts?

 

4. So the only solution to this is to run the scripts within Alfred?

 

Thanks!

Edited by kodiak

Share this post


Link to post
1 hour ago, kodiak said:

You're correct, my bad. Here's a link to the workflow (sorry for the mess, it's a work in progress).

https://www.dropbox.com/s/x0d4fpnia327oot/Menu Bar Icons.alfredworkflow?dl=0

 

The regular dropbox action works fine for me. The ⌥ version doesn’t: [ERROR: action.script] osascript: ./dropbox-option-down.scpt: No such file or directory. You haven’t included the script.

 

1 hour ago, kodiak said:

I see, if so, then why it doesn't happen in all scripts?

 

Because most scripting languages interpret the script instead of compiling it.

Share this post


Link to post
5 hours ago, kodiak said:

3. I see, if so, then why it doesn't happen in all scripts?

 

It does, as far as I can tell.

 

5 hours ago, kodiak said:

4. So the only solution to this is to run the scripts within Alfred?

 

No, Alfred runs them the normal way. And it's hardly a big issue anyway.

 

If you don't want the scripts to change, save them as .applescript (text) files instead of compiled scripts.

Share this post


Link to post
On 8/14/2018 at 2:16 PM, vitor said:

 

The regular dropbox action works fine for me. The ⌥ version doesn’t: [ERROR: action.script] osascript: ./dropbox-option-down.scpt: No such file or directory. You haven’t included the script.

 

Oops, my bad. I thought all workflow objects are included upon exporting it. Below is a link to the (revised) workflow file itself.

 

https://www.dropbox.com/s/4pmsrhnbv7l8wgd/Status Bar Icons.alfredworkflow?dl=0

 

 

 

Share this post


Link to post
2 hours ago, kodiak said:

Oops, my bad. I thought all workflow objects are included upon exporting it

 

They are. Not sure what happened there.


I get the following error: [ERROR: action.script] ./dropbox-option-down.scpt: execution error: «script» doesn’t understand the “«event ASTgetlo»” message. (-1708). The error is also reproducible when running the script from the Script Editor.

Share this post


Link to post

For the benefit of the OP, @kodiak, «event ASTgetlo» is the raw syntax code for AST mouse point location.  The error reported by @vitor relates to the commands from your scripting addition, which the script doesn't understand.  If you're trying to export a workflow, it won't be able to run on any system without the scripting addition included as part of the exported package.


That's one reason why using scripting additions is disadvantageous.


I can see you're trying to access the Dropbox menubar pop-up menu, which requires a modifier key to be depressed whilst clicking - something that can't be done conventionally with System Events.  However, if you consider which functions you want to access from that pop-up menu, I believe all of them are accessible through other means, namely the standard left-click pop-up pane, which has a preferences (cogwheel) button and a few other buttons that provide those options.  That will be regularly scriptable using System Events, if you really must go down that avenue.

Share this post


Link to post
On 8/16/2018 at 1:36 PM, vitor said:

 

They are. Not sure what happened there.


I get the following error: [ERROR: action.script] ./dropbox-option-down.scpt: execution error: «script» doesn’t understand the “«event ASTgetlo»” message. (-1708). The error is also reproducible when running the script from the Script Editor.

 

Sorry about that! The workflow has a dependency which I forgot to mention, it's the AppleScript toolbox: https://astoolbox.wordpress.com.

 

19 hours ago, CJK said:

For the benefit of the OP, @kodiak, «event ASTgetlo» is the raw syntax code for AST mouse point location.  The error reported by @vitor relates to the commands from your scripting addition, which the script doesn't understand.  If you're trying to export a workflow, it won't be able to run on any system without the scripting addition included as part of the exported package.


That's one reason why using scripting additions is disadvantageous.

 

You're totally right, but since I'm a complete workflow novice I had no idea how to include a dependency within a workflow, and install it upon the installation of the workflow itself (in this case, a simple copy to /Library/ScriptingAdditions/. And since I have the use mouse movement for this script to work, this addition has to be included. Nevertheless, I still can't explain why running the workflow within Alfred fails (unless I introduce delay into the script).

 

19 hours ago, CJK said:

I can see you're trying to access the Dropbox menubar pop-up menu, which requires a modifier key to be depressed whilst clicking - something that can't be done conventionally with System Events.  However, if you consider which functions you want to access from that pop-up menu, I believe all of them are accessible through other means, namely the standard left-click pop-up pane, which has a preferences (cogwheel) button and a few other buttons that provide those options.  That will be regularly scriptable using System Events, if you really must go down that avenue.

 

Actually, I have been considering this approach and indeed it does look like a better solution. I still wish however to resolve the current script's issues, just for the sake of the challenge ;)

 

P. S. - How do I insert those inline code blocks?

 

Thanks again!

Share this post


Link to post
[background=#eee][font=courier,monospace]sample inline code typeface[/font][/background]

should render as sample inline code typeface.

 

This font is Menlo

 

Edited by CJK

Share this post


Link to post
1 hour ago, kodiak said:

How do I insert those inline code blocks?

 

They’re not code blocks. Rather, it’s a text style change. I make them (and other text changes) via Markdown converted to BBCode with MarkdownTransform.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×