Launch file with string replace?

I am new to Keyboard Maestro. I have not had it two weeks now, and I am loving it already. I am looking around the forum and drinking in the knowledge from this great community, already it has been a help. Scripting sounds exciting (I know a good deal of JavaScript from web development), and I look forward to try some.
I have a question about constructing a macro. I have a need to launch files from a directory e.g. Users/pctechtv/Sites, that holds the files I test on my local PHP server. When the files are clicked on they would go to Firefox or any browser with this syntax in the first picture. I would like to know if it is possible to create a macro in Keyboard Maestro to launch the file in Firefox (or browser of choice) with a string replace to change them to what I need (second pic)? This way I won’t have to type them in manually to the address bar every time. I guess this string replace would have to read the file path and change it with regex or something.Thanks


Hey Chris,

Something like this?

You'll have to adjust the regex to prune your path strings appropriately, and you may or may not need to use the encode filter on the result.

-Chris


Generic-Test 01.kmmacros (5.6 KB)

This is a super helpful start. I can’t wait to test and configure it. Thanks

So after reading some more I see you and other members of community have already addressed some of my issues. I am referring to this thread.here There is talk around the idea that Pathfinder does not have good support for AppleScript in other posts. I understand that what I want to do is working with Pathfinder’s menu system so I am thinking the answer is no. But here it goes. Is there a way to turn a Macro like this into a Variable instead of a Clipboard entry? This way I won’t have an entry in the Clipboard (good for not cluttering the history). But if not I can live with it. I need to tweak it so Firefox can open and paste the URL better. It is working 9 out of 10 times. In Safari it is working 3 our 10 times. Thanks

If you want to avoid cluttering the clipboard, you could use the Delete Past Clipboatd action at the end of the macro.

Nice! That was so helpful. I know some other programs on Windows that need that option… Wow! I have only had this program a few days and it is blowing me away. Thanks

1 Like

A general process I follow that has helped me write macros that work 100% of the time in Safari (my browser of choice) and Path Finder (my file manager of choice):

  • Add 2s pauses wherever you think they might help.
  • Test: Does this work reliably?
    • No: Add more pauses and/or increase the duration of the pauses. Go back to "Does this work … ".
    • Yes: Reduce pauses by 0.3s until macro is no longer reliable. Then increase by 0.3s. Done.

And:

  • I generally create variables any time I need to filter or search-and-replace. “Set %Variable%MyNamedVariable% to Clipboard, operate on %Variable%MyNamedVariable%, paste %Variable%MyNamedVariable%” is, IME, more reliable than using just the system clipboard.

And (added after posting):

  • in Path Finder, I bind a key-chord to commands I use in KMacros, and execute them by having KM type. I cannot explain why, or if, this actually helps at all. IME, it seems to.

HTH.

—Kirby.

With your test method I am getting it to work. Great idea! Makes total sense. I don't understand what is meant by

Could you explain some more. Thanks

Hey Chris,

Look here:

(Note that it links to the actual post, so you can see all of it.)

If you're going to use Safari or Google Chrome you can talk to them directly and avoid clumsy paste operations.

tell application "Safari"
  activate
  set newDoc to make new document
  set URL of newDoc to "https://www.google.com"
end tell

So – to put things together:

------------------------------------------------------------
set myPrefix to "http://localhost/~pctechtv"
set AppleScript's text item delimiters to "/Users/pctechtv/Sites"

tell application "Path Finder"
  set selItemList to selection
  repeat with i in selItemList
    set contents of i to myPrefix & (text item 2 of (get POSIX path of (contents of i)))
  end repeat
end tell

tell application "Safari"
  activate
  repeat with i in selItemList
    make new document with properties {URL:i}
  end repeat
end tell
------------------------------------------------------------

Path Finder's AppleScript support is poor but serviceable for some things.

Safari on the other hand is quite scriptable.

-Chris

1 Like

Nice! Also works great with Firefox, had to change

make new document with properties {URL:i} 
to
open location i

You make we want to go and learn AppleScript today. Really cool, really powerful. Thanks so much.

By "key-chord" I mean what are generally known as "keyboard shortcuts" — the regular keyboard key which, pressed and released at the same time as one-to-four modifier keys, trigger an operation. One of PF's strengths is that it lets one easily assign key-chords to any menu command. This is done at "Path Finder ▹ Preferences ▹ Features ▹ Keyboard".

My suggestion to you is to replace the KM Action "Select Menu in {Path Finder}" with "Type Keystroke {key-chord}" after setting the key-chord in PF prefs to execute the menu item you want. (Of course, PF must be active when the Action "Type Keystroke {key-chord}" is executed.)

I do this with every application that I can. It is identical to the seemingly trivial case of using "⌘c" instead of "Edit ▹ Copy".

Again, I unable to claim that this helps, or explain why it might help — my experience has been that it does make my KMacros more robust. Some of these include a hundred Actions that operate on four applications and process hundreds of files, which seems to me to be complex enough to be a reasonable test of "robust-ness".

I have no doubt that scripting is a better approach, as long as one has a knowledgeable script-writer at hand :blush: .

I totally get what you mean and why, I have had similar experiences on Windows with macro programs. I have seen the simulation of keystroke presses or shortcut keys matter. Great to know about! Thanks

Hey Chris,

open location is not specific to Firefox. From the Applescript Language Guide:

“Opens a URL with the appropriate program.”

E.g. the default system app for that kind of URL.

I didn't know you could force it to talk to a specific app, but you can with an appropriate tell application block.

Interesting. :smile:

Firefox is not really scriptable otherwise. It's dictionary will allow moving windows around and resizing them but little else.

-Chris

Although Keyboard Maestro manages keyboard shortcuts well, it's generally safer to have it select a menu item than to activate a menu item's keyboard shortcut.

-Chris

I am not surprised to hear that about Firefox. I have some experience writing Mozilla Add-ons and I know they can be a funny animal. Could someone tell me what makes an applications AppleScript support "poor"? Is this something that the program developer has chosen to leave out. Is it that the program was not made in the typical way with Apple's core languages and frameworks like Cocoa and Objective C? I am very curious as I learn Apple's languages and technologies. Thanks

AppleScript support must be deliberately added, and many developers choose not to (damn it).

-Chris