Applescript problems with renaming a file and getting the alias to change it’s name as well

aliasapplescriptfinder

I have run into a particular problem with a larger script that I am writing. I am trying to rename all the files in a folder with a prefix of "temp_" and in doing so have the alias to that file still be addressable from AppleScript after the prefix has been added to the file. The following code works on SOME machines but not others. I have tried it on a 2018 MBP running 10.13, a 2012 Mac Mini running 10.13, and a 2015 iMac running 10.13 and 10.14. The iMac is the only machine that has problem with this code:

tell application "Finder"
set theSelection to the selection -- so you must have a folder with files selected in Finder before running this code.
set theFiles to the files of (item 1 of theSelection) --get the files
end tell
repeat with f from 1 to (count theFiles)
    set aItem to item f of theFiles -- set a variable as an individual file
    set pdfName to name of aItem --get the name of that file
    set aliasItem to aItem as alias -- get the alias of the file
    tell application "Finder"
        set the name of aliasItem to (("temp_" & (name of aliasItem)) as string) --rename the file
    end tell
    display dialog (aliasItem as string) buttons {"Cancel", "OK"} default button "OK" --this is just here for testing the script, to see what the system still thinks the file is named
    set posixPath to (POSIX path of aliasItem) --THIS is where the code fails on some machines. 
end repeat

The code fails on SOME machines when I try to convert the alias into a posix path. On the machines that this script works on, the test display dialog lists the file alias with the new name. So foo.txt becomes temp_foo.txt. On the machines that the script fails the display dialog comes back with just foo.txt as the file name.

On the machine on which this is failing, the file will be renamed temp_foo.txt like it should be, but the alias never updates, so it still thinks the file is just foo.txt. On the failing machines, the last command to set the variable to the posix path of the alias fails because the alias is incorrect since it is still pointing to the old file name.

If need be I will just rename all the files in one loop, then come back around and get the posix paths in another loop, since doing a new

set theTempFiles to files of (item 1 of theSelection)

Should get a new list of the files, now with their "temp_" prefix. But it pains me to put in a repeat loop that I don't think is really necessary since the code works FINE on some machines.

Who can help me to get my code to run fine, and who can get it to fail? I just would like to see how big of an issue this is. Also it would be nice if someone could tell me that I am doing something horribly wrong in my code, and that is why I am getting unpredictable results.

Best Answer

I think that got broken somewhere, as an alias is supposed to be a reference to a file item that remains valid even if it gets renamed or moved. If you are going to be using POSIX paths anyway, you might take a look at using Cocoa's NSURL class (via some AppleScriptObjC) to keep track of the file references, since that is ultimately what is getting used (or is supposed to be used).

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

tell application "Finder" -- get the selection
    set theSelection to selection
    set theFiles to the files of (first item of theSelection) as alias list
end tell

repeat with anItem in theFiles
    set anItem to POSIX path of anItem
    set fileRefURL to (current application's NSURL's fileURLWithPath:anItem)'s fileReferenceURL() -- get file reference from path
    tell application "System Events" to set the name of disk item anItem to "temp_" & (name of disk item anItem)
    set posixPath to (fileRefURL's |path|()) as text -- get path from file reference
    display dialog "Before:" & return & anItem & return & return & "After:" & return & posixPath buttons {"Cancel", "OK"} default button "OK"
end repeat