Here is the script I’ve been working on.
It takes two numbers as input (how do I do that with one dialog window?)
Then it locates the file, opens it, searches for the first number and replaces it with the second number.
Seems to work fine.
I can’t get the text edit file to save under the NewJobNumber.jlt on my desktop. Usually gives me a permission error. Suggestions?
tell application “Finder”
set MyDefaultPath to “Macintosh HD:Users:proofdepartment:Desktop:”
set my_dialog_result1 to display dialog “Job Number” default answer “”
set my_dialog_result2 to display dialog “New Job Number” default answer “”
set JobNum to the text returned of my_dialog_result1
set NewJobNumber to the text returned of my_dialog_result2
try
–looking in location 1
set ThisJob to “Office:1_Docs:JOHN:Public:Backup:template:” & JobNum & “.jlt”
duplicate file ThisJob to desktop
on error
try
–looking in location 2
set ThisJob to “Jobs:4colors:template:” & JobNum & “.jlt”
duplicate file ThisJob to desktop
on error
– couldn’t find file anywhere
display dialog “Can’t Find This Template”
end try
end try
end tell
tell application “TextEdit”
activate
– defining origin file and new file
set thisFile to “Macintosh HD:Users:proofdepartment:Desktop:” & JobNum & “.jlt”
set target_file to (((path to desktop folder) as text) & NewJobNumber & “.jlt”)
– opening origin file
open file thisFile
set theDoc to text of document 1
–searching for origin job number and replacing with new job number
searchReplace of me into theDoc at JobNum given replaceString:NewJobNumber
set the text of document 1 to result
close document 1 saving yes
end tell
on searchReplace into MainString at searchString given replaceString:replaceString
repeat while MainString contains searchString
set foundOffset to offset of searchString in MainString
set stringStart to text 1 through (foundOffset - 1) of MainString
set stringEnd to text (foundOffset + (count of searchString)) through -1 of MainString
set MainString to stringStart & replaceString & stringEnd
end repeat
return MainString
end searchReplace