Saving TextEdit file

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

Hi dklucznik.

Could you please use the Discourse formatting tags when posting AppleScript code here? I did it for you in your “Searching for a file, then copying it” thread a few days ago and provided a link to the Markdown Reference guide. The code needs to be posted as plain text, with straight quotes, and with three backticks on separate lines above and below the code:

```
AppleScript code

```

Thanks.

Sorry about that… here is the fixed code

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

One method to gather multiple user inputs from a ‘single’ dialog. Not really, but from the user’s perspective it will appear to be so.

  set fieldNameList to {"Firstname", "Lastname", "Pronouns"}
    User_GetMultiFieldUserInput(fieldNameList)
    
    on User_GetMultiFieldUserInput(fieldNameList)
        set inputValues to {}
        --prepare a list to hold user values.
        repeat length of fieldNameList times
            set the end of inputValues to ""
        end repeat
        --Loop through fields 
        repeat with i from 1 to length of fieldNameList
            set fieldName to item i of fieldNameList
            set dataSummary to ""
            --Build current data summary
            repeat with ii from 1 to length of fieldNameList
                set dataSummary to dataSummary & (item ii of fieldNameList & " : " & item ii of inputValues & return)
            end repeat
            --Request data
            set promptText to "Please provide the " & fieldName & return & return & dataSummary
            set defaultAnswer to item i of fieldNameList
            set userInput to ""
            --Don't take no for an answer
            repeat while userInput is in {"", defaultAnswer}
                set userInput to text returned of (display dialog promptText default answer defaultAnswer)
            end repeat
            --Store valid user data
            set item i of inputValues to userInput
            set userInput to ""
        end repeat
        --Maybe ask user to confirm their completed data?
        return inputValues
    end User_GetMultiFieldUserInput

good lord… I’ll try it… seems like a lot of code compared to just having two dialog windows…
my pressing problem would be saving the adjusted textEdit file

Here’s incorporating User_GetMultiFieldUserInput.

I don’t see any failure in your TextEdit code. Opens, edits and saves the file here. No templates or desktop folder structures here so can’t test the rest of your code.

tell application "Finder"
	set MyDefaultPath to "Macintosh HD:Users:proofdepartment:Desktop:"
	set {JobNum, NewJobNumber} to my User_GetMultiFieldUserInput({"Job Number", "New Job Number"})
	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


on User_GetMultiFieldUserInput(fieldNameList)
	set inputValues to {}
	--prepare a list to hold user values.
	repeat length of fieldNameList times
		set the end of inputValues to ""
	end repeat
	--Loop through fields 
	repeat with i from 1 to length of fieldNameList
		set fieldName to item i of fieldNameList
		set dataSummary to ""
		--Build current data summary
		repeat with ii from 1 to length of fieldNameList
			set dataSummary to dataSummary & (item ii of fieldNameList & " : " & item ii of inputValues & return)
		end repeat
		--Request data
		set promptText to "Please provide the " & fieldName & return & return & dataSummary
		set defaultAnswer to item i of fieldNameList
		set userInput to ""
		--Don't take no for an answer
		repeat while userInput is in {"", defaultAnswer}
			set userInput to text returned of (display dialog promptText default answer defaultAnswer)
		end repeat
		--Store valid user data
		set item i of inputValues to userInput
		set userInput to ""
	end repeat
	--Maybe ask user to confirm their completed data?
	return inputValues
end User_GetMultiFieldUserInput

If you have text that must be parsed, it would help if you gave representative examples.

Assuming that your job numbers are single strings of numbers and letters (e.g no spaces, no dashes), then you could use something like this.

When the dialogue is presented, the user should enter the two job numbers separated by a space, e.g.:

444a 555b

It simply grabs the words of the entered string and if there are exactly two, it uses the first as jobNum and the second as newJobNumber. FWIW, a comma would also work as the separator but it might complicate things to present that to your users. Note that the dash character is also considered a word separator so if your job numbers contain one, then you’ll need to take that into account.

set twoJobsDialogue to display dialog "Enter existing job number and new job number (separated by a space)" default answer ""

-- split entered string into two job numbers
set jobNumbers to text returned of twoJobsDialogue
--> "444a 555b" -- example string entered
set wc to count of words of jobNumbers
--> 2

if wc is 2 then
	set jobNum to word 1 of jobNumbers as text
	set newJobNumber to word 2 of jobNumbers as text
else
	display dialog "Two job numbers must be entered"
end if

-- just to see the combined results
{jobNum, newJobNumber}