I save service tickets as text files throughout the day. I have an AppleScript that I run that saves the ticket to the correct client folder and creates a file name including the date and a sequence number. This would be the name of a ticket that is the third ticket of the day created for client ABC:
I’d like the dialog box asking me for the ticket number for the day to pre-fill with the last ticket I entered for the day +1.
So if the above ticket was the last ticket I did, “03” would be saved somewhere, and when I’m saving a new ticket I’d like the dialog to read:
What is the ticket number for the day?
with default value being “04” (the last TicketNumber + 1)
So if I’m cranking out tickets, the dialog would give me the correct next ticket number most of the time and I’d just hit return to accept. If it is the next morning, and I did 5 tickets yesterday, I would just change the default in dialog from “06” to “01” and start over.
I can’t seem to figure out variables that survive outside the script running.
With recent versions of macOS, you cannot save the ticket number in the script. Alternatives are to save the ticket number to either a text file or plist. The following is the simplest approach:
set theFile to "/Users/Robert/Ticket Number.txt" -- set to desired path
try
set ticketNumber to read POSIX file theFile
set dialogNumber to 1 + ticketNumber
set dialogNumber to text -2 thru -1 of ("0" & dialogNumber)
on error
set dialogNumber to "00"
end try
set newNumber to text returned of (display dialog "What is the ticket number for the day?" default answer dialogNumber)
set newNumber to newNumber as integer
do shell script "echo " & newNumber & " > " & quoted form of theFile
I use preference scripts that save and get saved data from a preference file. The data is saved in the file as an AppleScript list
-- send the scripts a preference file name, it will be created in the User's Preferences folder.
on run
local prefsFile, subdomain, domain
set prefsFile to "com.maa.enom-ip.prefs" -- name of preference file
set_prefs(prefsFile, {"rmf", "maainsurance.com"}) -- save the preferences
set {subdomain, domain} to get_prefs(prefsFile) -- get the preferences
end run
on get_prefs(pFile as string)
local cfile, show_list
set cfile to (path to preferences as text) & pFile
try
set cfile to open for access cfile with write permission
on error
display alert "Fuck! Error opening file…" giving up after 10
return false
end try
try
set show_list to read cfile from 1 as list
on error
display alert "File Empty!" giving up after 10
write {} to cfile as list
set show_list to {}
end try
close access cfile
return show_list
end get_prefs
on set_prefs(pFile as string, pList as list)
local cfile
set cfile to (path to preferences as text) & pFile
try
set cfile to open for access cfile with write permission
on error
display alert "Fuck! Error opening file…" giving up after 10
return false
end try
try
set eof cfile to 0
write pList to cfile as list
on error
display alert "Error! Can't write to preference file…" giving up after 10
end try
close access cfile
return true
end set_prefs
FWIW, the following is a ASObjC solution that uses a preference file:
use framework "Foundation"
use scripting additions
set ticketNumber to readPlist()
if ticketNumber is missing value then set ticketNumber to 0
set dialogNumber to text -2 thru -1 of ("0" & (ticketNumber as integer))
set newNumber to text returned of (display dialog "What is the ticket number for the day?" default answer dialogNumber)
set newNumber to 1 + newNumber
writePlist(newNumber)
on readPlist()
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.eric.WorkNumber"
return theDefaults's objectForKey:"theKey"
end readPlist
on writePlist(theValue)
set theDefaults to current application's NSUserDefaults's alloc()'s initWithSuiteName:"com.eric.WorkNumber"
theDefaults's setObject:theValue forKey:"theKey"
end writePlist