Create text file to be read from on future runs

Hi,

I am wanting to make a small script where the user inputs information into a text box and that writes it to a text file in the background and saves it and on the next run in the text box it shows it already.

This is going to be apart of a bigger script that will be run multiple times and this value won’t need to change very often.

I have seen scripts to write to documents ect. but its visible to the user I’d rather it just happen in the back ground as it is quicker and nicer than having to wait for the script to create the document write to it then save it but I will it needing to be saved to the script directory which ultimately will be in excels AS folder.

So I am not sure if the script will need special permission to do so.

It would be great if someone can help!

Thanks,
Bruce

P.S.

The script I have already is here I just need to creating of the file and reading of the file in the background.

tell application "System Events" to set parentFolder to POSIX path of container of (path to me)
display dialog "Enter Batch Number:" default answer ""
set BatchNumber to text returned of result

Did a bit more googling and found something and tweaked it:

			
tell application "System Events" to set parentFolder to POSIX path of container of (path to me)
set filedata to read parentFolder & "/batch.txt" as «class utf8» using delimiter linefeed
display dialog "Enter Batch Number:" default answer filedata
set BatchNumber to text returned of result
set filename to "batch.txt"
set BatchFile to parentFolder & "/" & filename
do shell script "echo " & quoted form of BatchNumber & " > " & quoted form of BatchFile

although this does require the file to already exist and ahem data written to it and then when the dialog box opens emptying it and running it through again so it doesn’t error for a blank file.
But seen as I have already tinkered with it and is set to blank at the minuet without any problems so hopefully it won’t do it when I move it to the users computer as everything is contain within one folder.

If anyone knows a way of making this better would be nice to hear from you.

Sorry I posted and got an answer my self.

Thanks,
Bruce

P.S. sorry for the bump

Hey Bruce,

If System Events is not running on the user’s machine there can be a short (or noticeably long) pause while it starts up. Better to avoid it unless it is truly needed.

This script is a little more verbose, but it will be faster even so and has basic error-handling.


------------------------------------------------------------------------------
try
	
	set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set batchDataFile to ((text items 1 thru -2 of (path to me as text)) as text) & ":batch.txt"
	set AppleScript's text item delimiters to oldTIDS
	try
		set batchData to read alias batchDataFile as «class utf8»
	on error
		set batchData to ""
	end try
	display dialog "Enter Batch Number:" default answer batchData
	set batchNumber to text returned of result
	writeUTF8(batchNumber, batchDataFile)
	
on error e number n
	set e to e & return & return & "Num: " & n
	tell current application
		set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
	end tell
	if button returned of dDlg = "Copy" then set the clipboard to e
end try
------------------------------------------------------------------------------
on writeUTF8(_text, _file)
	try
		if _file starts with "~/" then
			set _file to POSIX path of (path to home folder as text) & text 3 thru -1 of _file
		end if
		set fRef to open for access _file with write permission
		set eof of fRef to 0
		write _text to fRef as «class utf8»
		close access fRef
	on error e number n
		try
			close access fRef
		on error e
			error "Error in writeUTF8() handler!" & return & return & e
		end try
	end try
end writeUTF8
------------------------------------------------------------------------------

Thanks for the response,
I ran it on its own and it worked fine but when I put it into the script where the other code was I get “expected else but found on”

Here is the code for the whole file:

tell application "System Events" to set parentFolder to POSIX path of container of (path to me)

display dialog "Select Order Number Cell" buttons {"Next", "End"}
if result = {button returned:"Next"} then
	tell application "Microsoft Excel"
		set OrderNumber to value of selection
	end tell
	display dialog "Select Product Code" buttons {"Next"}
	if result = {button returned:"Next"} then
		tell application "Microsoft Excel"
			set ProductCode to value of selection
		end tell
		display dialog "Select Amount" buttons {"Next"}
		if result = {button returned:"Next"} then
			tell application "Microsoft Excel"
				set CurtainAmount to value of selection
			end tell
		end if
		display dialog "Is This Correct " & OrderNumber & " " & ProductCode & " " & CurtainAmount buttons {"Yes", "No"}
		if result = {button returned:"Yes"} then

------------------------------------------------------------------------------
try
	
	set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set batchDataFile to ((text items 1 thru -2 of (path to me as text)) as text) & ":batch.txt"
	set AppleScript's text item delimiters to oldTIDS
	try
		set batchData to read alias batchDataFile as «class utf8»
	on error
		set batchData to ""
	end try
	display dialog "Enter Batch Number:" default answer batchData
	set batchNumber to text returned of result
	writeUTF8(batchNumber, batchDataFile)
	
on error e number n
	set e to e & return & return & "Num: " & n
	tell current application
		set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
	end tell
	if button returned of dDlg = "Copy" then set the clipboard to e
end try
------------------------------------------------------------------------------
on writeUTF8(_text, _file)
	try
		if _file starts with "~/" then
			set _file to POSIX path of (path to home folder as text) & text 3 thru -1 of _file
		end if
		set fRef to open for access _file with write permission
		set eof of fRef to 0
		write _text to fRef as «class utf8»
		close access fRef
	on error e number n
		try
			close access fRef
		on error e
			error "Error in writeUTF8() handler!" & return & return & e
		end try
	end try
end writeUTF8
--------			
			set filename to "WIS.laba"
			set labelfile to parentFolder & "/" & filename
			if ProductCode contains "**" then
				set HookType to "** "
			else if ProductCode contains "*" then
				set HookType to "** "
			else if ProductCode contains "**" then
				set HookType to "** "
			else if ProductCode contains "**" then
				set HookType to "** "
			end if
			if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			end if
			if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			end if
			
			tell application "Microsoft Word"
				activate
				create new document
				delay 1
				tell application "System Events"
					keystroke "Product Code: "
					keystroke tab
					keystroke "b" using command down
					keystroke ProductCode
					delay 1
					keystroke "b" using command down
					keystroke return
					delay 1
					keystroke "Order Number: "
					keystroke tab
					keystroke "b" using command down
					keystroke OrderNumber
					keystroke return
					delay 1
					keystroke "b" using command down
					delay 1
					keystroke "Batch Number: "
					keystroke tab
					keystroke "b" using command down
					keystroke BatchNumber
					keystroke return
					delay 1
					keystroke "b" using command down
					delay 1
					keystroke "Description: "
					keystroke tab
					keystroke tab
					keystroke "b" using command down
					keystroke HookType & CSize & CurtainColour
					
					
				end tell
			end tell
			
		else if result = {button returned:"End"} or result = {button returned:"No"} then
			display dialog "Re-run me" buttons {"OK"}
		end if
	end if
end if
end
end

sorry its a lot to dig through it highlights the on in “on writeUTF8(_text, _file)”

Hope you can help, I am new(-ish) to applescript and a lot of my code can be a bit of a hodge podge and using the long way round to something that could be made simple and more stable than what I have.

Thanks,
Bruce

Hey Bruce,

I don’t have time to test this out right now, so I’m just going to post it as is.

I’ll fiddle with it some more later, but you can give it a try with the changes I’ve made. It compiles at least.

Look at the end where I’ve put the handlers.

Search for: # ¢¢¢¢ ccs handler ¢¢¢¢

To see what I put within the body of your code.

Change the variable name batchNumber to match what you’ve done elsewhere.


tell application "System Events" to set parentFolder to POSIX path of container of (path to me)

display dialog "Select Order Number Cell" buttons {"Next", "End"}
if result = {button returned:"Next"} then
	tell application "Microsoft Excel"
		set OrderNumber to value of selection
	end tell
	display dialog "Select Product Code" buttons {"Next"}
	if result = {button returned:"Next"} then
		tell application "Microsoft Excel"
			set ProductCode to value of selection
		end tell
		display dialog "Select Amount" buttons {"Next"}
		if result = {button returned:"Next"} then
			tell application "Microsoft Excel"
				set CurtainAmount to value of selection
			end tell
		end if
		display dialog "Is This Correct " & OrderNumber & " " & ProductCode & " " & CurtainAmount buttons {"Yes", "No"}
		if result = {button returned:"Yes"} then
			
			# ¢¢¢¢ ccs handler ¢¢¢¢
			set batchNumber to getBatchNum()
			# ¢¢¢¢ ccs handler ¢¢¢¢
			
			set filename to "WIS.laba"
			set labelfile to parentFolder & "/" & filename
			if ProductCode contains "**" then
				set HookType to "** "
			else if ProductCode contains "*" then
				set HookType to "** "
			else if ProductCode contains "**" then
				set HookType to "** "
			else if ProductCode contains "**" then
				set HookType to "** "
			end if
			if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			else if ProductCode contains "**" then
				set CSize to "** "
			end if
			if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			else if ProductCode contains "**" then
				set CurtainColour to "**"
			end if
			
			tell application "Microsoft Word"
				activate
				create new document
				delay 1
				tell application "System Events"
					keystroke "Product Code: "
					keystroke tab
					keystroke "b" using command down
					keystroke ProductCode
					delay 1
					keystroke "b" using command down
					keystroke return
					delay 1
					keystroke "Order Number: "
					keystroke tab
					keystroke "b" using command down
					keystroke OrderNumber
					keystroke return
					delay 1
					keystroke "b" using command down
					delay 1
					keystroke "Batch Number: "
					keystroke tab
					keystroke "b" using command down
					keystroke batchNumber
					keystroke return
					delay 1
					keystroke "b" using command down
					delay 1
					keystroke "Description: "
					keystroke tab
					keystroke tab
					keystroke "b" using command down
					keystroke HookType & CSize & CurtainColour
				end tell
			end tell
			
		else if result = {button returned:"End"} or result = {button returned:"No"} then
			display dialog "Re-run me" buttons {"OK"}
		end if
	end if
end if

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on getBatchNum()
	set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set batchDataFile to ((text items 1 thru -2 of (path to me as text)) as text) & ":batch.txt"
	set AppleScript's text item delimiters to oldTIDS
	try
		set batchData to read alias batchDataFile as «class utf8»
	on error
		set batchData to ""
	end try
	set batchNumber to text returned of (display dialog "Enter Batch Number:" default answer batchData)
	writeUTF8(batchNumber, batchDataFile) of me
	return batchNumber
end getBatchNum
-------------------------------------------------------------------------------------------
on writeUTF8(_text, _file)
	try
		if _file starts with "~/" then
			set _file to POSIX path of (path to home folder as text) & text 3 thru -1 of _file
		end if
		set fRef to open for access _file with write permission
		set eof of fRef to 0
		write _text to fRef as «class utf8»
		close access fRef
	on error e number n
		try
			close access fRef
		on error e
			error "Error in writeUTF8() handler!" & return & return & e
		end try
	end try
end writeUTF8
-------------------------------------------------------------------------------------------

Hi,

I have updated my script a little (just to justify more information and to get the formatting of the output right) and when I run it I get the error No result was returned from some part of this expression. and highlights the “-2”.

Now it happens when I add a list in, if I remove the list it works fine, sadly this list is needed as it contains contact information that isn’t included in the spreadsheet where the other information is collected.

Could you see what could possibly be wrong?

sorry for pestering.


tell application "System Events" to set parentFolder to POSIX path of container of (path to me)

display dialog "Select Order Number Cell" buttons {"Next", "End"}
if result = {button returned:"Next"} then
	tell application "Microsoft Excel"
		set OrderNumber to value of selection
	end tell
	display dialog "Select Product Code" buttons {"Next"}
	if result = {button returned:"Next"} then
		tell application "Microsoft Excel"
			set ProductCode to value of selection
		end tell
		display dialog "Select Amount" buttons {"Next"}
		if result = {button returned:"Next"} then
			tell application "Microsoft Excel"
				set CurtainAmount to value of selection
			end tell
		end if
		(choose from list {"1", "2", "3", "4", "5"} with prompt "Some Text Here")
		if result = {"1"} then
			set PhoneNumber to "somephonenumber"
			set EmailAddress to "someemailaddress"
		else if result = {"2"} then
			set PhoneNumber to "somephonenumber"
			set EmailAddress to "someemailaddress"
		else if result = {"3"} then
			set PhoneNumber to "somephonenumber"
			set EmailAddress to "someemailaddress"
		else if result = {"4"} then
			set PhoneNumber to "somephonenumber"
			set EmailAddress to "someemailaddress"
		else if result = {"5"} then
			set PhoneNumber to "somephonenumber"
			set EmailAddress to "someemailaddress"
		end if
		
		display dialog "Is This Correct " & OrderNumber & " " & ProductCode & " " & CurtainAmount buttons {"Yes", "No"}
		if result = {button returned:"Yes"} then
			
			# ¢¢¢¢ ccs handler ¢¢¢¢
			set batchNumber to getBatchNum()
			# ¢¢¢¢ ccs handler ¢¢¢¢
			
			if ProductCode contains "**" then
				set HookType to "**"
			else if ProductCode contains "**" then
				set HookType to "**"
			else if ProductCode contains "**" then
				set HookType to "**"
			else if ProductCode contains "**" then
				set HookType to "**"
			end if
			if ProductCode contains "**" then
				set CSize to "**"
				set CAmount to "*"
			else if ProductCode contains "**" then
				set CSize to "** "
				set CAmount to "*"
			else if ProductCode contains "**" then
				set CSize to "** "
				set CAmount to "**"
			else if ProductCode contains "**" then
				set CSize to "**"
				set CAmount to "*"
			else if ProductCode contains "**" then
				set CSize to "**"
				display dialog "*****" default answer "0"
				set CAmount to text returned of result
				
			end if
			if ProductCode contains "**" then
				set Curtain to "**"
				set Colour to tab & tab & tab & tab & tab & tab & "**"
			else if ProductCode contains "**" then
				set Curtain to "**"
				set Colour to tab & tab & tab & tab & tab & tab & "**"
			else if ProductCode contains "**" then
				set Curtain to "**"
				set Colour to tab & tab & tab & tab & tab & tab & "**"
			else if ProductCode contains "**" then
				set Curtain to ""
				set Colour to "**"
			else if ProductCode contains "**" then
				set Curtain to ""
				set Colour to "**"
			else if ProductCode contains "**" then
				set Curtain to ""
				set Colour to "**"
			else if ProductCode contains "**" then
				set Curtain to ""
				set Colour to "**"
			end if
			
			
			tell application "Finder" to open POSIX file "/Users/**/Desktop/AUTO LABEL/relay.docx"
			delay 2
			tell application "System Events"
				keystroke "Product Code: "
				keystroke tab
				keystroke "b" using command down
				keystroke ProductCode
				delay 1
				keystroke "b" using command down
				keystroke return
				delay 1
				keystroke "Order Number: "
				keystroke tab
				keystroke "b" using command down
				keystroke OrderNumber
				keystroke return
				delay 1
				keystroke "b" using command down
				delay 1
				keystroke "Batch Number: "
				keystroke tab
				keystroke "b" using command down
				keystroke batchNumber
				keystroke return
				delay 1
				keystroke "b" using command down
				delay 1
				keystroke "Description: "
				keystroke tab
				keystroke "b" using command down
				keystroke HookType & CSize & Curtain & Colour
				keystroke return
				keystroke tab & tab & tab & tab
				keystroke CurtainAmount
				keystroke " IN A BOX"
				keystroke "b" using command down
				delay 1
				keystroke return
				keystroke "Call to re-order:"
				keystroke tab
				keystroke "b" using command down
				keystroke PhoneNumber
				delay 1
				keystroke "b" using command down
				keystroke return
				keystroke "Email:"
				keystroke tab
				keystroke "b" using command down
				keystroke EmailAddress
				keystroke "b" using command down
				keystroke "a" using command down
				keystroke "c" using command down
				keystroke (ASCII character 8)
				keystroke "q" using command down
				keystroke return
				
			end tell
			
		else if result = {button returned:"End"} or result = {button returned:"No"} then
			display dialog "Re-run me" buttons {"OK"}
		end if
	end if
end if

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on getBatchNum()
	set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
	set batchDataFile to ((text items 1 thru -2 of (path to me as text)) as text) & ":batch.txt"
	set AppleScript's text item delimiters to oldTIDS
	try
		set batchData to read alias batchDataFile as «class utf8»
	on error
		set batchData to ""
	end try
	set batchNumber to text returned of (display dialog "Enter Batch Number:" default answer batchData)
	writeUTF8(batchNumber, batchDataFile) of me
	return batchNumber
end getBatchNum
-------------------------------------------------------------------------------------------
on writeUTF8(_text, _file)
	try
		if _file starts with "~/" then
			set _file to POSIX path of (path to home folder as text) & text 3 thru -1 of _file
		end if
		set fRef to open for access _file with write permission
		set eof of fRef to 0
		write _text to fRef as «class utf8»
		close access fRef
	on error e number n
		try
			close access fRef
		on error e
			error "Error in writeUTF8() handler!" & return & return & e
		end try
	end try
end writeUTF8
-------------------------------------------------------------------------------------------

Thanks,
Bruce

Hey Bruce,

Let’s take this off-list so as to not clutter the board with more long scripts. At least until we have a solution.

listmeister@thestoneforge.com

I got your code to run through to here:

error “The variable HookType is not defined.” number -2753 from “HookType”

So I’m not sure where you’re having a failure.

Hi,

I sent you an email earlier with the files and applescript.

I had a bit of a play and released it works every other run for some reason I haven’t changed anything from what I sent you so I am not sure as to why it is doing this, hope you can help.

Thanks,
Bruce

EDIT:
The reason why it stops at the variable being undefined is just due to the fact I *ed out to anonimise (?) I have send you a complete copy that should work and the excel file is layed out in order of data selection.