I would like to create a macro to swap the names of two files selected in the Finder:
file_001.txt -> file_002.txt
file_002.txt -> file_001.txt
Any idea how to do this?
TIA
I would like to create a macro to swap the names of two files selected in the Finder:
file_001.txt -> file_002.txt
file_002.txt -> file_001.txt
Any idea how to do this?
TIA
Read my answer to this topic Duplicate, rename and increment the name of a file which has all the tools needed.
Basically, assuming you have exactly two files selected in the Finder, the first thing is to extract the two paths, do something like this:
Now it is a simple matter of renaming one file out of the way and the other file to the first, and then finally the temp file to the second name.
Assuming the files are always on your local harddisk, you can just do something like:
Move file %Variable%One% to ~/kmtempfile
Move file %Variable%Two% to %Variable%One%
Move file ~/kmtempfile to %Variable%Two%
If the files might be on a remote disk, then you will want to use the Get File Attribute action to get the parent path of one of the files and then use %Variable%Parent%/kmtempfile instead of ~ (~ is just your home directory).
Hey Cary,
I generally don’t like rewriting the files to change their names, so I’ll use AppleScript for something like this:
tell application "Finder"
set insertionLocation to insertion location as alias
set finderSelectionList to selection as alias list
if length of finderSelectionList ≠ 2 then
beep
error "No files were selected in the Finder!"
end if
set {fileOne, fileTwo} to finderSelectionList
set fileOneName to name of fileOne
set fileTwoName to name of fileTwo
set name of fileOne to fileOneName & "_tempfile"
set fileOne to fileOne as text
set name of fileTwo to fileOneName
set name of file fileOne to fileTwoName
update insertionLocation
end tell
Run from an Execute an AppleScript action.
-Chris
Hello,
Many thanks to both of you!
Christopher, I made a Finder macro with your AppleScript that works perfectly.
But, following Peter's instructions, I made a Path Finder macro that also works perfectly.
Swap Two Filenames [Path Finder].kmmacros (8.2 KB)
Thanks again!
Hey Cary,
Very good.
A few quick tests indicates it is swapping tags and Finder comments correctly, which is the main reason I defaulted to using the Finder. I’d want to test it more extensively, before I trusted it implicitly though.
A couple of suggestions:
A) I would consolidate the AppleScripts into one:
------------------------------------------------------------
tell application "Path Finder"
set pfSelectionList to selection
repeat with i in pfSelectionList
set (contents of i) to POSIX path of (contents of i)
end repeat
end tell
tell application "Keyboard Maestro Engine"
if length of pfSelectionList = 2 then
set AppleScript's text item delimiters to linefeed
set pfSelectionList to pfSelectionList as text
try
set value of variable "asErrorValue" to "Correct File Count!"
on error
make new variable with properties {name:"asErrorValue", value:"Correct File Count!"}
end try
try
set value of variable "pfFileList" to pfSelectionList
on error
make new variable with properties {name:"pfFileList", value:pfSelectionList}
end try
else
try
set value of variable "asErrorValue" to "Incorrect File Count!"
on error
make new variable with properties {name:"asErrorValue", value:"Incorrect File Count!"}
end try
end if
end tell
------------------------------------------------------------
Essentially I’m replacing your file counter with an error-value, and I’m setting the KM variables from the AppleScript.
B) I would either zero-out or delete the variables at the end of the macro to minimize the changes of side-effects from leftover data.
-Chris
Hey Chris,
Thanks for your suggestions! But, if I understand why you recommend to empty the variables at the end of the script, I don’t understand the interest of setting KM variables from the AppleScript. What’s exactly the point of this script against the other?
Thank you for the clarification.
Hey Cary,
Running two AppleScripts takes significantly more time than running one.
If you’re going to run an AppleScript to begin with you might as well do as much as you can with it.
It saves time and complexity.
In your small macro this is not of great importance, but in a more complex macro it might well be.
* Note: KM macros can be triggered from AppleScript.
-Chris
Hey Chris,
Thank you for that clarification. You’re right, simplicity is the new complexity