Optimising Macro Action - Applescript

Hiya,

I'm trying to optimise this macro which is used on numerous occasions it takes 0.46 seconds to complete. Can anyone offer any advice on how I can speed this up?

tell application "Daylite"
	set currentObjectName to eval "(selectedObjects lastObject) name."
	set getSelectedObjectEntityName to eval "(selectedObjects lastObject) entity name."
end tell

tell application "Keyboard Maestro Engine"
	setvariable "Current Object Name" to currentObjectName
	setvariable "Get SelectedObject Entity Name" to getSelectedObjectEntityName
end tell

tell application "System Events"
	tell process "Daylite"
		get name of menu item 14 of menu 1 of menu bar item 4 of menu bar 1
	end tell
end tell

tell application "Keyboard Maestro Engine"
	make variable with properties {name:"Current Project Name", value:result}
end tell

Many thanks in advanced for your assistance.

I don't use Daylite, so it's hard to tell from your post alone if I'm even testing it with relevant data, but I did download it and made a quick test project, and running your script as-is from Keyboard Maestro takes an average of 0.12 seconds on my system. Modifying the script slightly to this:

tell application "Daylite"
	set currentObjectName to eval "(selectedObjects lastObject) name."
	set getSelectedObjectEntityName to eval "(selectedObjects lastObject) entity name."
end tell

tell application "System Events"
	tell process "Daylite"
		set currentProjectName to (get name of menu item 14 of menu 1 of menu bar item 4 of menu bar 1)
	end tell
end tell

tell application "Keyboard Maestro Engine"
	setvariable "Current Object Name" to currentObjectName
	setvariable "Get SelectedObject Entity Name" to getSelectedObjectEntityName
	setvariable "Current Project Name" to currentProjectName
end tell

to consolidate commands a bit more and running it as a compiled script file instead of a text script brings the average time down to 0.1 seconds for me, so you might see a similar bit of optimization by doing the same (here's the script file to make things a bit easier: Daylite Test.scpt.zip (4.8 KB)

However, running even the non-tweaked script in Script Debugger, I consistently get times of only 0.03 seconds. While it doesn't make practical sense to run this script from Script Editor or Script Debugger every time you want to execute it, it may be worth trying out FastScripts and running this script from there instead of from KM to see if that results in any speed increase.

Hi gglick, thank you for your response. My computer still takes 0.46 to compile the modified script. We won’t be able to run this as a file or externally.

When I use the following script running it from Script Editor it takes 0.074 seconds. That's much faster compared to KM. How can I integrate this so that multiple computers can use this script and fast? connected to the same network but triggered in KM.....

set mgRightNow to "perl -e 'use Time::HiRes qw(time); print time'"
set mgStart to do shell script mgRightNow
---------------------------------------
tell application "Daylite"
   set currentObjectName to eval "(selectedObjects lastObject) name."
   set getSelectedObjectEntityName to eval "(selectedObjects lastObject) entity name."
end tell

tell application "System Events"
   tell process "Daylite"
      set currentProjectName to (get name of menu item 14 of menu 1 of menu bar item 4 of menu bar 1)
   end tell
end tell

tell application "Keyboard Maestro Engine"
   setvariable "Current Object Name" to currentObjectName
   setvariable "Get SelectedObject Entity Name" to getSelectedObjectEntityName
   setvariable "Current Project Name" to currentProjectName
end tell
---------------------------------------
set mgStop to do shell script mgRightNow
set mgRunTime to mgStop - mgStart
display dialog "This took " & mgRunTime & " seconds." & return & "that's " & (round (mgRunTime * 1000)) & " milliseconds."

Here are some untested ideas:

  1. Use FastScripts to assign a keyboard shortcut to the compiled script file.
  2. Then either run the script directly, or use KM to type the shortcut.
  3. Compile and save the script as an app. Run this app directly or from KM.

Hi JMichaelTX,

Thank you for your response.

  1. The KM macros are used on 7 different iMacs. I'm reluctant to use any other software other than KM to ensure that the same macros are shared on every computer to prevent any further errors from arising.

  2. I have held the script on DropBox and triggered it as a script action in KM, it still takes 0.46 seconds. Dropbox is needed to enable all iMacs to access latest version of the scripts.

  3. Same DropBox problem as point 2. above.

If KM could improve how it handles AppleScript then my macros can go from taking 0.46 seconds to 0.074 per AppleScript action.

Is there anything else you suggest I should consider?

Thank you for your time.

Regards,

I think @peternlewis has made it clear that he does not see any way of speeding up the KM execution of AppleScripts, and has no plans to do so. So, for the foreseeable future you will need to accept KM performance for what it is.

To be honest, I have to ask what does this matter? 0.46 sec is still very fast compared to the time it would take a human to do the task. The fact is that the large majority of the time the computer is waiting for the user to do something.

So, what is the real issue here? Why are you so concerned about < 1/2 sec?

Keyboard Maestro runs AppleScripts via the osascript shell command.

So for text, it is basically "save the text in a temp file", then osascript file, and for files, it is basically osascript file.

Comparing it to what AppleScript is doing when AppleScript is pre-compiling it and executing it from memory is pointless. Keyboard Maestro will not execute AppleScripts within its own process because AppleScript crashes. So the script is executed in its own process, via osascript, so the speed is however fast osascript can execute it.

Work on optimising that and see how you go with that.

0.46 seconds is very fast, but we have 1,400 macros. Some macros trigger other macros which in turn trigger others. Some macros can contain up to 8 ApplesScripts, I have attempted to consolidate them but my ability is limited.

In essence there is a lot of waiting around for the AppleScripts to complete when it could potentially be done much faster.

When you consider the number of different AppleScripts which are needed to run then 0.074 to 0.46 seconds per AppleScript really is a major difference.

Is KM faster at processing Object-C or Swift? if so, I don't know either of these languages I will need to learn.

Keep in mind that AppleScripts can execute KM Macros and Actions. So you could have a system that minimizes the number of KM Execute AppleScripts, having the KM Macro that is triggered at the start of your workflow only call one master AppleScript, which then calls other KM Macros and AppleScript handlers as needed. And be sure to maximize use of ASObjC.

If you don't already have Script Debugger 7 I would highly recommend it. Among other great features, it comes with a number of very useful ASObjC Clippings that make using ASObjC much easier.

1 Like

Thank you, will consider it's features shortly.