Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to change name of file with AppleScript

Hi All


I’m looking for help regarding a simple apple script. But I'm new to this and is stuck..

The purpose of the script is to change a preferences file, and open a software app.


I ran into a problem when I try to get the script to rename a file.


I need to use POSIX due to this is supposed to be a generic script, and the files are located in the users folder.


I use exactly the same structure as this example to duplicate and delete a fil, that works fine.


But it doesn't workwhen I will have the script to change name of a file.


Pleas help me out with this.


---

tell application "Finder"


set name of document "preference_TEST.xml" to (POSIX path of (path to home folder)) & "Library/Preferences/MPP/Local Store/preferences.xml" as POSIX file


end tell

MacBook Air 13″, macOS 10.14

Posted on Oct 28, 2020 6:25 AM

Reply
Question marked as Best reply

Posted on Oct 28, 2020 11:43 AM

>I need to use POSIX due to this is supposed to be a generic script, and the files are located in the users folder.


Completely sure I don't understand this statement. You're using Finder references, so there's no value in POSIX paths. POSIX paths should be used when using shell commands.


In any case, this is broken in many ways:


> set name of document "preference_TEST.xml" to (POSIX path of (path to home folder)) & "Library/Preferences/MPP/Local Store/preferences.xml" as POSIX file


Consider what you're asking the Finder to do.


First off you have some vague reference to 'document "preference_TEST.xml"' without giving the Finder a hint as to where to find this document. In addition, the Finder doesn't really have 'documents' of its own, so this will fail on that reference alone. Instead you should use 'file' or 'document file' instead (intrinsically, 'documents' are owned by specific applications, but in this case the Finder doesn't own any of these files, it just manipulates them).


Then (assuming the Finder can actually find this file), you're trying build the path to a some other presences file. In your example:


> POSIX path of (path to home folder)) & "Library/Preferences/MPP/Local Store/preferences.xml"


would translate into something like:


/Users/username/Library/Preferences/MPP/Local Store/preferences.xml


which you then try to coerce to a POSIX file (hint: you can't change a filename into a POSIX file)

In either case, even dropping the POSIX file faux pas, this script would try to change the FILENAME of the file to that full path - this DOES NOT move the file in any way, just changes its name. So assuming the original preferences_TEST.xml file was, say, on your desktop, this would rename:


~/Desktop/preferences_TEST.xml


to:


~/Desktop/\/Users\/username\/Library\/Preferences\/MPP\/Local\ Store\/preferences.xml


which I'm 99.9% sure isn't what you want - renaming a file doesn't move it - it leaves it where it is and just changes its name.


Reading between the lines, you want your script to take a specific preferences_TEST.xml file and move it into the appropriate users Preferences folder, replacing any existing file, is that correct? Do you want/need to retain the original _TEST.xml file? or not.


Assuming not, your script should look something more like:


set dT to (path to desktop) -- this returns the current user's desktop path
set dP to (path to preferences) -- this returns the current user's preferences folder
tell application "Finder"
	-- find the source file
	set prefsFile to file "preferences_TEST.xml" of dT
	-- rename it
	set name of prefsFile to "preferences.xml"
	-- and move it into place. Use 'duplicate' instead of 'move' to leave the source file in place
	move prefsFile to folder "Local Store" of folder "MPP" of dP with replacing
end tell


Similar questions

5 replies
Question marked as Best reply

Oct 28, 2020 11:43 AM in response to Tacoface

>I need to use POSIX due to this is supposed to be a generic script, and the files are located in the users folder.


Completely sure I don't understand this statement. You're using Finder references, so there's no value in POSIX paths. POSIX paths should be used when using shell commands.


In any case, this is broken in many ways:


> set name of document "preference_TEST.xml" to (POSIX path of (path to home folder)) & "Library/Preferences/MPP/Local Store/preferences.xml" as POSIX file


Consider what you're asking the Finder to do.


First off you have some vague reference to 'document "preference_TEST.xml"' without giving the Finder a hint as to where to find this document. In addition, the Finder doesn't really have 'documents' of its own, so this will fail on that reference alone. Instead you should use 'file' or 'document file' instead (intrinsically, 'documents' are owned by specific applications, but in this case the Finder doesn't own any of these files, it just manipulates them).


Then (assuming the Finder can actually find this file), you're trying build the path to a some other presences file. In your example:


> POSIX path of (path to home folder)) & "Library/Preferences/MPP/Local Store/preferences.xml"


would translate into something like:


/Users/username/Library/Preferences/MPP/Local Store/preferences.xml


which you then try to coerce to a POSIX file (hint: you can't change a filename into a POSIX file)

In either case, even dropping the POSIX file faux pas, this script would try to change the FILENAME of the file to that full path - this DOES NOT move the file in any way, just changes its name. So assuming the original preferences_TEST.xml file was, say, on your desktop, this would rename:


~/Desktop/preferences_TEST.xml


to:


~/Desktop/\/Users\/username\/Library\/Preferences\/MPP\/Local\ Store\/preferences.xml


which I'm 99.9% sure isn't what you want - renaming a file doesn't move it - it leaves it where it is and just changes its name.


Reading between the lines, you want your script to take a specific preferences_TEST.xml file and move it into the appropriate users Preferences folder, replacing any existing file, is that correct? Do you want/need to retain the original _TEST.xml file? or not.


Assuming not, your script should look something more like:


set dT to (path to desktop) -- this returns the current user's desktop path
set dP to (path to preferences) -- this returns the current user's preferences folder
tell application "Finder"
	-- find the source file
	set prefsFile to file "preferences_TEST.xml" of dT
	-- rename it
	set name of prefsFile to "preferences.xml"
	-- and move it into place. Use 'duplicate' instead of 'move' to leave the source file in place
	move prefsFile to folder "Local Store" of folder "MPP" of dP with replacing
end tell


Oct 28, 2020 11:26 AM in response to Niel

Hi


Unfortunately it does not help. I get the following error message.


error "Finder got an error: Can’t set document \"preference_TEST.xml\" to name of alias \"Macintosh HD:Users:chrpoi:Library:Preferences:MPP:Local Store:preferences.xml\"." number -10006 from document "preference_TEST.xml"


I have tried to set an alias for the folder, and after that change the name of the file... But I'm not successful in that either.

Oct 29, 2020 8:43 AM in response to Camelot

Camelot. Big thanks.


It worked after a small change.

Script can't move "prefs File" as its name was changed from what it first was set to be.


I corrected the script with to "set" lines:

set prefsFile_Test to file "preferences_TEST.xml" of dT


As it now is renamed I set:

set prefsFile to file "preferences.xml" of folder of dT


Then it moved nicely to where I wanted it :)


Like this:

set dT to (path to desktop) -- this returns the current user's desktop path

set dP to (path to preferences) -- this returns the current user's preferences folder


tell application "Finder"

-- find the source file

set prefsFile_Test to file "preferences_TEST.xml" of dT

-- rename it

set name of prefsFile_Test to "preferences.xml"

set prefsFile to file "preferences.xml" of dT

-- and move it into place. Use 'duplicate' instead of 'move' to leave the source file in place

move prefsFile to folder "Local Store" of folder "MPP" of dP

end tell

Oct 29, 2020 9:34 AM in response to Tacoface

> Script can't move "prefs File" as its name was changed from what it first was set to be.


Ahh, good point. The joys of Finder scripting :)

Theoretically I could/should have used an alias reference, which is a pointer to a file object that is persistent as it is moved/renamed, etc.


Either way, glad I could at least point you in the right direction.

How to change name of file with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.