Feature Request: Option to Assign new path to a variable when Moving/Renaming a file

Hi – I’ve got a macro that uses an AppleScript to choose a file and assign its POSIX path to a variable, then sends that variable to KM.

Once in KM, the macro appends the file’s original path to another variable (i.e. pathList), then moves/renames the file to another folder – after which I want to append this new path to the pathList variable.

Obviously this is eminently doable by assigning the new folder location to a variable, and assigning the Name of my original file to another variable, then merging these two, then adding it to my pathList variable.

I wondered if it might be possible, though, to add an option to the Move/Rename File action, whereby you can assign the new/resulting path to a variable if you wish to do so? (in my case I would re-assign it to my pre-existing filePath variable, which I would then append to the bottom of my pathList variable.)

So, as this would save me a couple of steps and a couple of additional variables in my current macro, I thought perhaps others might have uses for it and that I should request/suggest it as a contender for a future feature.

Thanks for considering, Peter.

Its certainly possible (and indeed I would find it useful myself which is always good for a feature request).

I will note it down, but it probably wont make it into 7.0 (I have to start getting brutal on the todo list or it’s not going to be finished until next year!).

Hah! No doubt.

In that vein, would you prefer that enthusiasts such as myself refrain from barfing out in real time every feature request that occurs to us? Perhaps saving them up for a later date, or some such? Or would you prefer we just fire away?

Very much looking forward to 7.0.

Feature requests are always appreciated. If I haven’t heard of it then I don’t know someone needs it, and it may give me a lead on something else as well. Alternatively, if I have heard of it, it helps me gauge how widely it would be used which is a factor in deciding which features to implement.

Any time is fine - most new features now will be deferred until after 7 unless they happen to tie in with something else.

Probably by email to support would be the best venue rather than the forum unless you are looking for other people to chime in with either “me too” or alternative solutions.

Hey Sam,

Since you’re going out to AppleScript already it might be more efficient to stay there through the adding to the pathList variable step.

set destFolder to alias ((path to home folder as text) & "test_directory:mv_destination:")
set moveIt to choose file
set moveItPosix to POSIX path of moveIt

set newFileName to "Nuts4!.txt"

tell application "Finder"
  set newFile to (move moveIt to destFolder) as alias
  set name of newFile to newFileName
end tell

set newFilePath to POSIX path of ((destFolder as text) & newFileName)

tell application "Keyboard Maestro Engine"
  set value of variable "x" to moveItPosix & return & newFilePath & return
end tell


Best Regards,
Chris

1 Like

Thanks very much, Chris – it’s a helpful idea and I appreciate you taking the time to offer it.

Ironically I had originally done the whole workflow in AppleScript. My hatred for working with aliases/files/posix paths/posix files and what have you (inside and outside of Finder tell blocks) in AppleScript – combined with my desire to both become more proficient in my use of Keyboard Maestro and to migrate towards some more KM-centric workflows – is what led me here with this issue.

FWIW, I’ve noticed a bunch of the AppleScript and Regex solutions you’ve posted for others, and I’m learning a lot from them – so thanks again for all your help.

Great to know – thanks very much, Peter.

Hey Sam,

Nothing wrong with wanting to become more proficient with KM. I regularly eschew an AppleScript solution I can crank out in a couple of minutes for a chance to stretch my KM skills - when I have the time and energy.

A couple of rule-of-the-thumbs for AppleScript:

ALWAYS use aliases unless you specifically need a Posix path for the shell (or the very rare app that insanely doesn't work with aliases).

Aliases are almost universally portable.

ALWAYS use as alias or as alias list in the Finder when working with the selection or a whose clause.

These are always much faster than Finder references.

NOTE: Occasionally the odd whose clause won't coerce to an alias list.

Alias lists can be moved or copied en-mass in the Finder.

There are other little pitfalls of course, but I've stuck to these rules as much as possible for close to 20 years.

--
Best Regards,
Chris

Great tips, Chris. I’ve saved them as a note and so I can refer back when necessary.

I definitely find it confusing working with paths and aliases in AppleScript. All seems to go well for me if just working with Aliases (taken from Finder selections or ‘Choose’ dialogs) and POSIX paths for Shell Scripts. Going back and forth between those two seems to work out okay for me.

Once variables with paths stored as strings and paths stored as files get into the mix as well, I start to lose track of what’s going on. Things I think I should be able to coerce to Aliases, I am unable to. Things I think I should be able to coerce to POSIX paths, I am unable to. Things I think I should be able to coerce to POSIX files or POSIX files as aliases, I am unable to. And things I think I should be able to convert to Quoted Form of, I am unable to.

Eventually I always get things to work, but the fact that I don’t at all times have a full grasp on exactly what form the value of the variable is currently in, and when I can and cannot coerce that value to my desired form, bugs the crap out of me sometimes.

Anyhoo, just a little rant about how challenging I find working with files and file paths and Finder in AppleScript. Should note, thought, that I am pretty much a novice, and my script-writing comes in bursts – between which I forget much of what I learned before.

Thanks again for the tips and assistance.

(Edited to Add…) Just after writing the above it occurred to me that I have a concrete question I could ask you:

In one of your earlier posts you suggest “Abbreviating paths with Tilde”. The rationale made sense to me, so I tried it in a macro I’m working on. When I brought those paths back to my AppleScript though, I think I had trouble coercing them to other forms (either Aliases or POSIX paths or both). If you have info on this I’d love to hear it. Thanks.

Hey Sam,

Yes. Always use a "relative" path if at all possible.

Absolute paths will break when you change hard drive names, user names, rebuild your system, or move to someone else's system.

The Finder does not grok Posix paths, however System Events does:

(System Events also understands home-folder notation.)

set _dir to "~/Documents/"

tell application "System Events"
  set expandedDir to POSIX path of (disk item _dir)
end tell

# Note that these are outside of SEV.
set expandedDirAlias to alias POSIX file expandedDir
set expandedDirAlias to (POSIX file expandedDir) as alias

I try to avoid using SEV for several reasons, most notably because it is often quit by Mavericks/Yosemite and can cause delays in scripts/macros when it starts up. So I only use it when it is the best tool for the job.

You can set it to NOT quit though, and I always do in scripts that employ it.

tell application "System Events"
  if quit delay is not 0 then set quit delay to 0
end tell

For home-folder paths I use my own handler to expand tilde:

set _dir to "~/Documents/"
set expandedDir to expandTilde(_dir)

on expandTilde(_path)
  if _path is "~" or _path is "~/" then
    set _path to POSIX path of (path to home folder as text)
  else if _path starts with "~/" then
    set _path to (POSIX path of (path to home folder as text)) & text 3 thru -1 of _path
  else
    error "Bad path string!"
  end if
end expandTilde

It's faster than SEV anyway.

Here's how to coerce a posix path to an alias:

set posixPathSpaces to "/Users/chris/test_directory/Posix_Path_Test/Test File With Spaces.txt"
set posixPathNoSpaces to "/Users/chris/test_directory/Posix_Path_Test/Test_File_Without_Spaces.txt"

posixPathSpaces as alias # FAILS
posixPathNoSpaces as alias # FAILS

(POSIX file posixPathSpaces) as alias # WORKS
(POSIX file posixPathNoSpaces) as alias # WORKS

Here's what I call a relative alias:

set myRelativeAlias to alias ((path to documents folder as text) & "Code_Library:")

If you spend any time with AppleScript you should belong to the Applescript Users List and MacScripter.net.

ASUL Archive

Apple's search is broken, so you have to use Google:

Your-Search-Terms site:http://lists.apple.com/archives/applescript-users

Did I answer your question? :smile:

-ccs

Hah! Assume stuff, Chris – thanks very much.

[EDIT: ‘Assume’ should have read ‘Awesome’. Hmmm.]

All of the info useful, but the highlight for me is (POSIX file pathVar) as alias to deal with all paths – spaces or no spaces. That illuminates some of my past troubles.

Much appreciated yet again. And I’ll look into the resources/groups you mention.

All the best.