You need a temporary filename to do the swap.
For two files A, B:
set name of A to temname
set name of B to A
set name of temname to B
This can be done using Finder scripting along these lines:
But that turns out to be unreliable because the Finder likes to work on things asynchronously.
I use a shell script to accomplish the task. It’s robust enough to function inside a tight loop.
This example is written to swap the names of two files (Aaa and Bbb) in a folder named “result” located on the Desktop
The subroutine ShellSwapFileNames(YOfilpth, YNfilpth, YYfilpth) takes 3 file paths, the location of the two files whose names you want to swap, and the location where it should keep the temp file. I just leave that in the same folder as the other two files.
set YOfilpth to (path to desktop as string) & "result:Aaa.txt"
set YNfilpth to (path to desktop as string) & "result:Bbb.txt"
set YYfilpth to (path to desktop as string) & "result:tem.txt"
ShellSwapFileNames(YOfilpth, YNfilpth, YYfilpth)
return
on ShellSwapFileNames(YOfilpth, YNfilpth, YYfilpth)
set retval to true
set YOPOSIX to POSIX path of YOfilpth
set YNPOSIX to POSIX path of YNfilpth
set YYPOSIX to POSIX path of YYfilpth
--display dialog YOPOSIX & "\n" & YNPOSIX & "\n" & YYPOSIX
--return retval
-- This method chains the swap parts into 1 shell proc, sequence execution order SHOULD be assured
-- This works in my test script inside a 200 repeat loop, so it seem pretty solid.
-- Trying such a loop with Finder scripting can bring the System to its knees.
try
do shell script "mv " & quoted form of YOPOSIX & " " & quoted form of YYPOSIX & ";mv " & quoted form of YNPOSIX & " " & quoted form of YOPOSIX & ";mv " & quoted form of YYPOSIX & " " & quoted form of YNPOSIX
on error
set retval to false
end try
return retval
end ShellSwapFileNames
tell application "Finder"
set file_1 to "Path:To:File 1" -- define path to File 1
set x to name of file file_1
set file_2 to "Path:To:File 2" -- define path to File 2
set y to name of file file_2
-- rename each file using a leading space:
set name of file file_1 to " " & x
set name of file file_2 to " " & y
-- define path to newly renamed files:
set temp_file_1 to "Path:To: Temp File 1" -- don't forget the leading space before Temp File 1!
set temp_file_2 to "Path:To: Temp File 2" -- don't forget the space!
set name of file temp_file_1 to y
set name of file temp_file_2 to x
end tell
I’m a bit out of my depth here as I’m still on Tiger and have no experience with creating Automator Services. However, I was able to create an Automator workflow which does what I believe you’re trying to achieve. The workflow is comprised of a singleRun AppleScript action, which I saved as a Plug-in for the Finder with the name “Swap File Names.” When two items (and only two items) are selected in the Finder the workflow can be accessed through a right-click contextual menu.
I have no idea what kind of tweaking, if any, would be necessary to get the script to work in systems beyond 10.4.11, nor can I tell you how to create a Service. My guess is that it shouldn’t be too difficult. Good luck!
Here’s the script that was used:
on run {input, parameters}
tell application "Finder"
set sel to selection
if (count of sel) ≠2 then
display dialog "You must select a total of two items." buttons {"OK"} default button 1 cancel button 1 with icon stop
else
set file_1 to item 1 of sel
set file_2 to item 2 of sel
end if
set x to name of file_1
set y to name of file_2
set name of file_1 to " " & x
set name of file_2 to " " & y
set temp_sel to selection
set temp_file_1 to item 1 of temp_sel
set temp_file_2 to item 2 of temp_sel
set name of temp_file_1 to y
set name of temp_file_2 to x
end tell
return input
end run
Incidentally, the workflow I set up for myself uses only one action: Run AppleScript. I didn’t need to include Get Selected Finder Items – the script itself does all the work!
Of course, your results may vary. Thanks for posting back.