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

How to rename a file after creating it in AppleScript?

Hi everybody, I got a little problem with a applescript. I got a script that created a text document into my active Finder folder. The only problem is that I dont know how to label the document after creating it - meaning « New document » without the suffix and with immediate possibility to rename it. For example: « New document.txt » -> « School.txt » .


For example: Finder -> secondary action -> New Folder --- it creates New Folder as follows:


User uploaded file

Now I can rename the folder without clicking on the folder. This is what I need for my text document after creating it.


Thanks, Ales.

Posted on Mar 5, 2014 2:02 AM

Reply
8 replies

Mar 5, 2014 7:07 AM in response to Frank Caggiano

Hi! Thank you for your answer. I must have explained myself wrong. It is something else that I want to do. I have a script which I use as an app to create a new text document in every Finder window. So I have an icon there, and by clicking on it, it creates new text document, an another one,.. and another one… doc+1. Like this :


User uploaded file


So what I want is renaming the « new text doc » after on cluck on the app icon just like we do it with the « new folder ». It is similar to creating a new txt doc in Windows : I create it, it has a default name and I can rename it. The default name is highlighted so I can change it instantly. Like this:


User uploaded file


That is what I want with the text document. On the other hand I want to preserve the possibility to leave the name as it was when created.


My well known code:


tell application "Finder"

try

set fileName to "New text doc"

set thisFolder to the target of the front window as alias

tell application "System Events"

set fileList to get the name of every disk item of thisFolder

end tell

set newFile to fileName

set fileVersion to 1

repeat

if newFile is in fileList then

set newFile to fileName & fileVersion

set fileVersion to fileVersion + 1

else

exit repeat

end if

end repeat


set createdFile to make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}


on error errMsg

display dialog (errMsg)

end try

endtell

Mar 5, 2014 7:25 AM in response to mr.ale

Ok I think I under stand now.


The best i can come up with is to add reveal createdFile after the file is created

set createdFile to make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}


revealcreatedFile

This will place the selection on the newly created file but it will not be in edit mode you will still need to click it to edit it. But at least it is selected.


I can't think of anyway to have the new filename put into edit mode via Applescript.

Mar 5, 2014 7:56 AM in response to mr.ale

Something like this


set createdFile to make new file at thisFolder with properties {name:newFile, file type:"TEXT", creator type:"ttxt"}



revealcreatedFile

try

display dialog "Rename File" default answernewFilebuttons {"Cancel", "OK"} default button

onerror

return

end try


set name of createdFile to text returned of result


will put up a dialog and ask if you wnt to rename the file.

Mar 6, 2014 1:13 PM in response to mr.ale

More correctly - it depends.


It depends entirely upon the application and whether it's attachable (i.e. offers the ability to attach scripts to objects in the UI, and customize the UI. Few applications do it, so in most cases it isn't possible to do, but there are exceptions.


Unfortunately, Contacts.app is in the former category, so there's no way you can change its UI/actions. The closest you can get is via the Services menu.

Mar 6, 2014 2:26 PM in response to mr.ale

Thanks a lot. You are right, this is one of the possibilities that has come to my mind, but unfortunately I don’t find it as effective.

Have you actually tried it in a running script? I hesitated to mention that but once I tried it it seemed to flow ok.


With the newly created file getting the selection and the ability to cancel and keep the original name it didn't feel forced to me. If you haven't actually run this for a while you should you might be surprised.



And when you come down to it it is either this or having the selection on the new file and having to click it to edit. There isn't a third option as far as I can see.

How to rename a file after creating it in AppleScript?

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