Editing Multiple AppleScript Macros

I have to update about 30 macros which are all same apart from folder address in the AppleScript.

Any suggestions as to how I might update IP address, year, client ID last field)?

Thanks Michael

Watersons Save Sketch.kmmacros (7.6 KB)

Hey Michael

That's actually pretty easy. The following script operates on the selected-macro in the Keyboard Maestro Editor.

It also makes the assumption that there's only one Execute AppleScript Action in the given macro, so if any of your macros have more than one let me know.

If your various macros are easy to find then probably wouldn't be difficult to make the script find them all and update them all en masse.

NOTE – make sure to test on a copy or test macro, before jumping in...


# Auth: Christopher Stone
# dCre: 2023/01/04 06:40
# dMod: 2023/01/04 06:40 
# Appl: Keyboard Maestro, Keyboard Maestro Engine
# Task: Replace String in AppleScript Action XML Using RegEx.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Keyboard_Maestro_Engine

set replacePat to "/Volumes/ Files 2021 /WE Watersons"

tell application "Keyboard Maestro"
   set selectedMacro to item 1 of (get selected macros)
   set applescriptAction to first action of selectedMacro whose name contains "AppleScript"
   set actionXML to xml of applescriptAction
   set newXML to my kmCng:"/Volumes/.+" inStr:actionXML withStr:replacePat usingRegEx:true usingCase:false usingTokens:false
   tell applescriptAction to set its xml to newXML
end tell

--» kmCng()
--  Task: Find and Replace with RegEx using Keyboard Maestro's AppleScript search command.
--  dMod: 2019/01/19 04:33
# Uses the Keyboard Maestro Engine for RegEx Support.
on kmCng:regExPat inStr:dataStr withStr:replacePat usingRegEx:regExBool usingCase:caseBool usingTokens:tokensBool
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for regExPat replace replacePat ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmCng:inStr:withStr:usingRegEx:usingCase:usingTokens:
1 Like

Have to get my head round this.

There are 2 AS scripts as far as I can see.

Not even sure what to ask :slight_smile:
Copied and pasted first AS - nothing, Ditto with second.

Not sure how to use this. Obviously appreciate help - just a bit over my head :frowning:

It's just one script -- the second "chunk" is a Handler (a function or subroutine in other languages), and you can see it being called in the set newXML line of the first "chunk".

Paste the whole thing into Script Editor/Script Debugger and run it with a test macro selected in KM.

However -- it looks like you are going to have to make similar changes every year, so it might be worth looking at other ways of doing this. For example, have Global variables for IP address and year which you can use in all 30 macros, have a local variable in each macro for client ID, build the path as a KM variable which you then use in the AppleScript.

I also strongly suggest you get rid of the space at the end of the folder name -- that could bite you hard if you ever have need to access it via SMB from a Windows box...

1 Like

Whups... Looks like I got overly task-focused.

I'll post a proper script that improves the function of Default Folder and uses a global Keyboard Maestro variable to store the path to the folder in question.

But first...

@michael_gilbert – please explain what you're exporting to PDF from Preview. There might be a better way.

1 Like

Worked. Just needed a bit of synapse activation. Cheers.

I need "Save as…" as Menu selection in Preview.

Assumed needed ⌥ Key but listed in KM! Duh!

So no need for export after all.

I answered this in the other post you posted.

Launch Preview. Then in a Show Menu Action, there is a drop-down on the right side labeled Menu. Click that and go to Preview, the 'save as' is there for selecting.

Do consider @ccstone's question about the overall purpose of these macros -- Preview is nicely AppleScriptable and you can probably avoid all the menu selects and delays and whatever altogether and just tell Preview to "save this document in this place". Much quicker and, importantly, much more robust than something that relies on UI interactions...

1 Like

That's fine, but I'm more interested in what you're doing in the first place.

What exactly are you opening in Preview to save as pdf?