Jump to content

NSApplescript vs. Run Script


Recommended Posts

In the workflow panel it's stated that the NSApplescript action runs on Alfred's main thread and that background Applescripts should be run with the Run Script command. What are the actual implications of that difference? In what situations would you use one over the other?

Edited by Subject22
Link to post

In the workflow panel it's stated that the NSApplescript action runs on Alfred's main thread and that background Applescripts should be run with the Run Script command. What are the actual implications of that difference? In what situations would you use one over the other?

 

Essentially, the Run NSAppleScript set to 'compiled' will be the fastest, and for fast non-processing type AppleScripts, it is perfect BUT this will make Alfred use a little more memory. NSAppleScript will also allow you to easily show dialogs from your AppleScript as it doesn't have the same security restrictions as osascript. Due to the nature of NSAppleScript, you need to wrap your AppleScript in on alfred_script(q) and end alfred_script.

 

Using a Run Script with osascript runs the AppleScript in the background which will allow a script to take quite some time to run and not block Alfred (i.e. you can use Alfred while something is still running in the background from a previous workflow). AppleScript can be pasted raw into this and doesn't need any special wrapping.

Link to post

Gotcha :) The blocking behaviour is a useful point to note. So what would you recommend as a 'default' method of running Applescripts? Default to NSApplescript (set to compiled) unless the script takes any noticeable time to run? Or default to Run Script unless the script needs the extra speed? Essentially what I'm saying is that there are many scripts for which the method of execution won't matter, and I'm wondering if one method is better than the other for such scripts.

Edited by Subject22
Link to post

Essentially, the Run NSAppleScript set to 'compiled' will be the fastest, and for fast non-processing type AppleScripts, it is perfect BUT this will make Alfred use a little more memory. NSAppleScript will also allow you to easily show dialogs from your AppleScript as it doesn't have the same security restrictions as osascript. Due to the nature of NSAppleScript, you need to wrap your AppleScript in on alfred_script(q) and end alfred_script.

 

Using a Run Script with osascript runs the AppleScript in the background which will allow a script to take quite some time to run and not block Alfred (i.e. you can use Alfred while something is still running in the background from a previous workflow). AppleScript can be pasted raw into this and doesn't need any special wrapping.

 

It’s very interesting.

 

Is there any known workflow that takes advantage of the extra speed? I’d like to study it and make some tests. Thank you.

Link to post

Gotcha :) The blocking behaviour is a useful point to note. So what would you recommend as a 'default' method of running Applescripts? Default to NSApplescript (set to compiled) unless the script takes any noticeable time to run? Or default to Run Script unless the script needs the extra speed? Essentially what I'm saying is that there are many scripts for which the method of execution won't matter, and I'm wondering if one method is better than the other for such scripts.

 

 

It’s very interesting.

 

Is there any known workflow that takes advantage of the extra speed? I’d like to study it and make some tests. Thank you.

 

It's the latency you may see as different - the 'compiled' option will keep the AppleScript pre-compiled in memory and run immediately... so AppleScript will start to run in a fraction of a second after asking it to.

 

Without compiling (or using Run Script with osascript), it has to compile the script before running it. On modern Macs, this is still pretty fast though.

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
×
×
  • Create New...