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.

  • Why not delete the alias, rename the original file then create a new alias using the correct new filename?
    – Solar Mike
    Nov 22, 2019 at 18:47
  • It is not a true alias like you would see in the file system. In AppleScript there are several ways to reference files, and alias is one of those ways. Not sure how better to describe it. So where the script fails is trying to convert that "alias" in a posix path, but the alias (reference to the file) has not had the name of the file updated in the reference itself. So the "alias" still refers to the old name of the file. Nov 22, 2019 at 19:35
  • Let me guess - the Mojave machine is the one with the problem? An alias is supposed to be a reference to the file that won't change even if it is moved or renamed, but it looks like it got broken somewhere. If you are ultimately going to be using POSIX paths, you might take a look at using NSURL via some AppleScriptObjC.
    – red_menace
    Nov 22, 2019 at 20:41
  • Well one of the machines is a 10.13 machine that is having problems. I just tried the script on 3 different 10.12 machines and it worked on all of them. But I have a VM that I have tried on two different machines with 10.15 on it, and the script failed on the VM on two different machines (2018 MBP and 2015 iMac). Can you change the name of a file via the NSURL in ASObjC? I know very little about ASObjC, and am a little intimidated by it. Nov 22, 2019 at 20:59
  • You can use regular AppleScript for the name change stuff, and just use NSUrl to keep track of the file references. I'll post an answer with an example.
    – red_menace
    Nov 22, 2019 at 21:12

1 Answer 1


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .