KM Variable as path in Applescript?

I feel a bit sheepish for not being able to figure out something so basic on my own, but here we are.

I'd like to move the Finder selection to a destination path as set by TriggerValue, using AppleScript so that it can be undone if need be. This is as far as I've managed to get but, for reasons that will doubtless be obvious to you, nothing happens:

The path passed on as a TriggerValue is copied directly from Finder, as as such is formatted like this example:

/Users/(username)/Desktop/File.doc

I don't know whether that format needs to be changed; I'm kind of flying blind.

Context: I have hundreds of files that need to be sorted and I'd like to press a button on my Stream Deck to send each one to one of a number of folders in the same directory. If there's a simpler method, please do let me know.

Side note: I vaguely remember there being a way to get AppleScripts to look all pretty in KM. I'm sure there's a hotkey you press and it compiles them with colours on a white background but I can't remember what it is and couldn't find it by searching on here. [Edit: Solved below by @Tiffle. It's Enter on the Numpad or fn+Enter.]

Just press the Enter key on your numpad while you’re editing your AS…

2 Likes

Aha! I don't have a numpad, so it's fn+Enter for me. Thanks!

1 Like

I don't suppose you happen to know the solution to the main question do you?

After a bit more searching, perhaps this is more along the right lines? (Doesn't work though.)

Sorry Neil - I’m only good at AppleScript when I can find a solution using Google or this forum mixed in with a bit of guesswork.

Nice-looking script, though :wink:

BTW - why not use native KM actions instead?

I can achieve the moving part with native KM actions, of course. The issue is that there's no undo. If you use AppleScript, Finder keeps track of everything and makes undo steps available. I know that you can pass variables to AppleScript and that you can use it to move files, so I'm hoping it's not too tricky to make this work. If it's a nightmarish can o' worms, I'll use KM actions and just try not to be a butter-fingers with the Stream Deck!

Sorry - I missed the bit about the undo.

I haven't tested it, but wouldn't moving a file by using a Shell command be undo-able? If so, you can pass the command to Shell instead of AppleScript.

I don't know how to do that either, unfortunately.

Someone here will be able to help you. I'm in the middle of a tech phone call with someone in real life so I can provide only this short example while I'm on hold:

image

Yeah that doesn't mean much to me, but I appreciate the attempt.

Hey Neil,

That ought to work...

You should be testing in the (Apple) Script Editor.app or preferably Script Debugger. (Remember that the commercial version reverts to a freeware "lite" version after a trial period.)

This script is basically the same as yours but made a trifle more robust, and it now handles tilde-based Posix-Paths.

If this doesn't work then you've got a boo boo somewhere.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/11/24 22:04
# dMod: 2021/11/24 22:04 
# Appl: Finder, Keyboard Maestro Engine
# Task: Move the Selected Items in the Finder to the given destination folder.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Keyboard_Maestro_Engine, @Move, @Selected, @Items
--------------------------------------------------------
use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------

tell application "Keyboard Maestro Engine"
   set FileDest to getvariable "FileDest"
end tell

set FileDest to ((current application's NSString's stringWithString:FileDest)'s stringByExpandingTildeInPath) as text
set destinationAlias to alias POSIX file FileDest

tell application "Finder"
   set selectedItemList to selection as alias list
   move selectedItemList to destinationAlias
end tell

--------------------------------------------------------

-Chris

2 Likes

Nailed it Chris, as usual. Nice one! This is going to save me a lot of time once I've set up the buttons.

1 Like