Save TextEdit document with another name if already exists

Hi everybody,

I wrote this (my first) script that

1.creates a folder and subfolder in the Documents folder if they don’t already exist
2. pastes the clipboard to a new TextEdit document
3. sets the font to Helvetica 20
4. Manual entry of the file name
5. saves and closes the document
6. manual entry of Spotlight comments

If there already is a file with the manually entered name in the subfolder I want to choose to overwrite the existing document or give the the document another name. How can I do that?

Is there a way to add tags for 10.9 and later manually in a dialog window?

This is my first script and I am pretty sure that there are simpler solutions.

Thank you.

-- create a folder and subfolder in the Documents folder if they do not already exist

set my_folder to ((path to documents folder as text) & "My Folder:")
set my_subfolder to ((path to documents folder as text) & "My Folder:My Subfolder:")
tell application "Finder"
	if not (exists my_folder) then
		make new folder at folder "Documents" of home with properties {name:"My Folder"}
	end if
	if not (exists my_subfolder) then
		make new folder at folder "My Folder" of folder "Documents" of home with properties {name:"My Subfolder"}
	end if
end tell

-- make a new TextEdit document, paste the clipboard and set the font to Helvetica 20

tell application "TextEdit"
	launch
	delay 1
	activate
	make new document at the front
	set text of front document to the clipboard
	set font of text of front document to "Helvetica"
	set size of text of front document to 20
	
	-- Manually type the name of the document in a dialog window
	
	set newDoc to text returned of (display dialog "Enter name" default answer "my name" with title "Save document" buttons {"Save"} default button 1 with icon 1)
	set newDoc to my_subfolder & newDoc & ".rtf" as text
	
	save document 1 in file newDoc -- save and close document
	
	-- I want a prompt if a document with the same name already exists. If it already exists there should be a dialog window where I can choose to overwrite the existing document or to give it another name. How can I do that?

	close front document
end tell

-- manually add Spotlight comments to the saved document

tell application "Finder"
	activate
	set comment of file newDoc to text returned of (display dialog "Enter comments, separated by comma" default answer "Comment" with title "Enter Comments" buttons {"Save"} default button 1 with icon 1)
end tell

-- How to enter additional tags for 10.9. and later?

You may try this one :

-- create a folder and subfolder in the Documents folder if they do not already exist

set my_folder to ((path to documents folder as text) & "My Folder:")
set my_subfolder to ((path to documents folder as text) & "My Folder:My Subfolder:")
tell application "Finder"
	if not (exists my_folder) then
		make new folder at folder "Documents" of home with properties {name:"My Folder"}
	end if
	if not (exists my_subfolder) then
		make new folder at folder "My Folder" of folder "Documents" of home with properties {name:"My Subfolder"}
	end if
end tell

-- make a new TextEdit document, paste the clipboard and set the font to Helvetica 20

tell application "TextEdit"
	launch
	delay 1
	activate
	make new document at the front
	set text of front document to the clipboard
	set font of text of front document to "Helvetica"
	set size of text of front document to 20
	
	-- Manually type the name of the document in a dialog window
	set suffix to ""
	set num to 1
	repeat
		set newName to text returned of (display dialog "Enter name" default answer "my name" & suffix with title "Save document" buttons {"Save"} default button 1 with icon 1)
		set newDoc to my_subfolder & newName & ".rtf"
		tell application "System Events" to set maybe to exists disk item newDoc
		if maybe then
			set whatToDo to button returned of (display dialog "What to do ?" buttons {"OverWrite", "New Name"} default button 2)
			if whatToDo is "OverWrite" then exit repeat
			
		else
			exit repeat
		end if
		set num to num + 1
		set suffix to "_" & num
	end repeat
	save document 1 in (newDoc as «class furl») -- save and close document
	
	-- I want a prompt if a document with the same name already exists. If it already exists there should be a dialog window where I can choose to overwrite the existing document or to give it another name. How can I do that?
	
	close front document
end tell

-- manually add Spotlight comments to the saved document

tell application "Finder"
	activate
	set comment of file newDoc to text returned of (display dialog "Enter comments, separated by comma" default answer "Comment" with title "Enter Comments" buttons {"Save"} default button 1 with icon 1)
end tell

-- How to enter additional tags for 10.9. and later?

For the tags question, look at :
http://macscripter.net/viewtopic.php?id=41892
http://macscripter.net/post.php?tid=44072

Yvan KOENIG (VALLAURIS, France) vendredi 10 juillet 2015 21:33:28

Thank you very much Yvan, that worked.
I added a delay after setting the font-size as the script did not work when it run the first time and there was no folder created yet.
I added another delay before setting the comments as there was a bouncing Finder icon with a beep without the delay (only on Yosemite, not on Mac OS 10.8.5).

-- create a folder and subfolder in the Documents folder if they do not already exist

set my_folder to ((path to documents folder as text) & "My Folder:")
set my_subfolder to ((path to documents folder as text) & "My Folder:My Subfolder:")
tell application "Finder"
	if not (exists my_folder) then
		make new folder at folder "Documents" of home with properties {name:"My Folder"}
	end if
	if not (exists my_subfolder) then
		make new folder at folder "My Folder" of folder "Documents" of home with properties {name:"My Subfolder"}
	end if
end tell

-- make a new TextEdit document, paste the clipboard and set the font to Helvetica 20

tell application "TextEdit"
	launch
	delay 1
	activate
	make new document at the front
	set text of front document to the clipboard
	set font of text of front document to "Helvetica"
	set size of text of front document to 20
	delay 1
	
	-- Manually type the name of the document in a dialog window
	set suffix to ""
	set num to 1
	repeat
		set newName to text returned of (display dialog "Enter name" default answer "my name" & suffix with title "Save document" buttons {"Save"} default button 1 with icon 1)
		set newDoc to my_subfolder & newName & ".rtf"
		
		--Prompt if a document with the same name already exists. If it already exists show dialog window and choose to overwrite the existing document or to give it another name. 
		
		tell application "System Events" to set maybe to exists disk item newDoc
		if maybe then
			set whatToDo to button returned of (display dialog "What to do ?" buttons {"OverWrite", "New Name"} default button 2)
			if whatToDo is "OverWrite" then exit repeat
			
		else
			exit repeat
		end if
		set num to num + 1
		set suffix to "_" & num
	end repeat
	save document 1 in (newDoc as «class furl») -- save and close document
	close front document
end tell

-- manually add Spotlight comments to the saved document

tell application "Finder"
	activate
	delay 0.5
	set comment of file newDoc to text returned of (display dialog "Enter comments, separated by comma" default answer "Comment" with title "Enter Comments" buttons {"Save"} default button 1 with icon 1)
end tell

I’m surprised because I tested it under Yosemite.
Maybe the pasted text was too short on my side to need the delay.

When I have a doubt, I don’t insert a predefined delay.
I insert this kind of code :

set oldSize to -1
tell application "System Events"
	repeat
		if size of disk item newdoc = oldSize then exit repeat
		set oldSize to size of disk item newdoc
		delay 0.1
	end repeat
end tell

Shane Stanley posted a better code using ASObjC but I have no time available to search itin my archives.

Yvan KOENIG (VALLAURIS, France) samedi 11 juillet 2015 19:03:23

Thank you Yvan, perfect!

-- create a folder and subfolder in the Documents folder if they do not already exist

set my_folder to ((path to documents folder as text) & "My Folder:")
set my_subfolder to ((path to documents folder as text) & "My Folder:My Subfolder:")
tell application "Finder"
	if not (exists my_folder) then
		make new folder at folder "Documents" of home with properties {name:"My Folder"}
	end if
	if not (exists my_subfolder) then
		make new folder at folder "My Folder" of folder "Documents" of home with properties {name:"My Subfolder"}
	end if
end tell

-- make a new TextEdit document, paste the clipboard and set the font to Helvetica 20

tell application "TextEdit"
	launch
	delay 1
	activate
	make new document at the front
	set text of front document to the clipboard
	set font of text of front document to "Helvetica"
	set size of text of front document to 20
	delay 1
	
	-- Manually type the name of the document in a dialog window
	
	set suffix to ""
	set num to 1
	repeat
		set newName to text returned of (display dialog "Enter name" default answer "my name" & suffix with title "Save document" buttons {"Save"} default button 1 with icon 1)
		set newDoc to my_subfolder & newName & ".rtf"
		tell application "System Events" to set maybe to exists disk item newDoc
		if maybe then
			set whatToDo to button returned of (display dialog "What to do ?" buttons {"OverWrite", "New Name"} default button 2)
			if whatToDo is "OverWrite" then exit repeat
			
		else
			exit repeat
		end if
		set num to num + 1
		set suffix to "_" & num
	end repeat
	
	save document 1 in (newDoc as «class furl») -- save and close document
	close front document
	
end tell

-- manually add Spotlight comments to the saved document

tell application "Finder"
	activate
	set oldSize to -1
	tell application "System Events"
		repeat
			if size of disk item newDoc = oldSize then exit repeat
			set oldSize to size of disk item newDoc
			delay 0.1
		end repeat
	end tell
	set comment of file newDoc to text returned of (display dialog "Enter comments, separated by comma" default answer "Comment" with title "Enter Comments" buttons {"Save"} default button 1 with icon 1)
end tell

Thanks for the feedback.

Yvan KOENIG running Yosemite 10.10.4 (VALLAURIS, France) lundi 13 juillet 2015 16:56:36