We have a Xerox colour printer at work that I would like to keep a log on how many prints and when I run to it. I thought Textedit and an Applescript may be one way to do this.
Here’s the script:
-- Enter time stamp
set time_stamp to do shell script "date +'%Y-%m-%d %H:%M:%S'"
set date_header to "Date: " & time_stamp
-- text entry to variable for adding to text file
set JobNo to text returned of (display dialog "Please enter job number" default answer "")
set JobDescription to text returned of (display dialog "Please enter Job Description" default answer "")
set Printouts to text returned of (display dialog "Number of Printouts?" default answer "0")
-- TotalPrints as number
-- if TotalPrints is not equal to 0 then
-- set TotalPrints to 0
-- end if
-- set TotalPrints to (TotalPrints + Printouts)
set NewLine to JobNo & " " & JobDescription & return & "Prints: " & Printouts & return
-- & "Total Printouts: " & TotalPrints
-- target file variable
set thelogfile to ((path to desktop as text) & "Xerox log.rtf")
-- open target file
tell application "TextEdit"
activate
open thelogfile
end tell
-- move cursor to end of file & paste in text
tell application "System Events" to tell process "TextEdit"
keystroke (ASCII character 31) using command down
keystroke return & return & date_header & return & NewLine
end tell
tell application "TextEdit"
save document 1
display dialog "close document?" buttons {no, yes} default button 2
if the button returned of the result is "yes" then
-- set QuitApp to (button returned of (display dialog "close document?" buttons {no, yes} default button 2))
--if ((QuitApp as string) is (yes as string)) then
quit application "TextEdit"
end if
end tell
2 things that I’m getting struck on :
How do I define the variable “TotalPrints” as a number equalling zero for the first run of the script but then have the it store the current number to be updated each time the script is run. (I’m currently getting “TotalPrints is not defined”.)
The script worked initially, when in the last section I entered “delay 5” then “quit” to pause Textedit prior to exiting, to actually view what had been entered. I would prefer to have a dialog box (see omitted script lines) that prompts the user then quits on response to a dialog button. But I can’t get this to work !!
Thanks in Advance.
Ps. Thanks also macscripter for a great online resource, you’ve help me get this far!
First, Matt, AppleScript Properties are “remembered”. Second, AppleScript can write a text file directly, so you don’t need to fool around with TextEdit:
property TotalPrints : 0
-- Enter time stamp
set time_stamp to do shell script "date +'%Y-%m-%d %H:%M:%S'"
set date_header to "Date: " & time_stamp
-- text entry to variable for adding to text file
set JobNo to text returned of (display dialog "Please enter job number" default answer "")
set JobDescription to text returned of (display dialog "Please enter Job Description" default answer "")
set Printouts to text returned of (display dialog "Number of Printouts?" default answer "0")
-- TotalPrints as number
-- if TotalPrints is not equal to 0 then
-- set TotalPrints to 0
-- end if
set TotalPrints to (TotalPrints + Printouts) -- not sure of your logic here.
set NewLine to JobNo & " " & JobDescription & return & "Prints: " & Printouts & return
-- & "Total Printouts: " & TotalPrints
-- target file variable
set thelogfile to ((path to desktop as text) & "Xerox log.rtf")
set XLF to open for access thelogfile with write permission
try
write return & date_header & return to XLF
write NewLine to XLF
close access XLF
on error e
close access XLF
display dialog "There was an error: " & e
end try
Hi Adam,
on the issue of TotalPrints property am I right in thinking that this will only work as long as the script isn’t closed? Next time the script is run after being closed the TotalPrints would be back to 0 again? If this is the case I’ve created a very basic preferences file that just requires a text file with a 0 in it called TotalPrints.txt being stored in the users preferences folder. Every time the script is run it’ll pickup the last TotalPrints value. Is my logic correct or is there a better way of doing this, I have used a shell script to create a preference before but this might do just as well? Please correct me if I’m wrong.
Thanks
set mypath to (path to preferences as string) & "TotalPrints.txt"
set mypathalias to mypath as alias
set TotalPrintsCount to read mypathalias
set TotalPrints to TotalPrintsCount + 1 as string
write TotalPrints to file mypath
display dialog TotalPrintsCount
Try this script. Run it several times, during which the value of N will increment. Then quit . Open it and run it again. Then close the script window, saving it somewhere (desktop for easy trial). Double click it to open in your script editor and run it again. N continues to increment. (All true for 10.4, not so sure about Jaguar)
property N : 0
set N to N + 1
display dialog "N = " & N
I’ve just noticed a couple of other issues with the script, for what they’re worth.
Firstly, the result of the shell script is Unicode text, so that’ll the class of the first write to the file. TextEdit will still be able to open the file (with Stefan’s modification), but there’ll be an invisible character between each character “ as you’ll see if you move through the text with the left and right arrows. The simplest remedy is to coerce the shell script result immediately to string.
Secondly, each write to a newly opened file starts at the beginning. I imagine that the idea with the log file is for it to be ‘grown’, in which case it should be written to ‘starting at eof’.
property TotalPrints : 0
-- Enter time stamp
set time_stamp to (do shell script "date +'%Y-%m-%d %H:%M:%S'") as string -- NB. Coerce to string.
set date_header to "Date: " & time_stamp
-- text entry to variable for adding to text file
set JobNo to text returned of (display dialog "Please enter job number" default answer "")
set JobDescription to text returned of (display dialog "Please enter Job Description" default answer "")
set Printouts to text returned of (display dialog "Number of Printouts?" default answer "0")
-- TotalPrints as number
-- if TotalPrints is not equal to 0 then
-- set TotalPrints to 0
-- end if
set TotalPrints to (TotalPrints + Printouts) -- not sure of your logic here.
set NewLine to JobNo & " " & JobDescription & return & "Prints: " & Printouts & return
-- & "Total Printouts: " & TotalPrints
-- target file variable
set thelogfile to ((path to desktop as Unicode text) & "Xerox log.txt")
set XLF to open for access thelogfile with write permission
try
write return & date_header & return to XLF starting at eof -- NB. Start first write to just-opened file at eof.
write NewLine to XLF
close access XLF
on error e
close access XLF
display dialog "There was an error: " & e
end try
If instead the log file’s meant to be written from the beginning every time, insert a line ‘set eof XLF to 0’ between the ‘try’ and the first ‘write’ line, and omit ‘starting at eof’. This’ll stop stray characters appearing at the end of the text if the latest write’s shorter than a previous one.
Except when a script’s run in, or saved from, Script Editor, the current values of its properties and globals are saved back into the script file at the end of each run. This has been the case since the early days of AppleScript. (Not that I go that far back myself, you understand. ) However, there was an issue with early versions of Tiger, where this didn’t always happen. I forget when it was fixed, but there are probably some release notes on line somewhere.
Nigel - you resolved the issue of End of File before I even got the response posted ! - ta.
The script works as required. Updated (and annotated) script as follows:
-- Printout Log for Xerox - Saving to Text file --
property TotalPrints : 0
-- Enter time stamp
set time_stamp to (do shell script "date +'%Y-%m-%d %H:%M:%S'") as string -- NB. Coerce to string.
set date_header to "Date: " & time_stamp
-- text entry to variable for adding to text file
set JobNo to text returned of (display dialog "Please enter job number" default answer "")
set JobDescription to text returned of (display dialog "Please enter Job Description" default answer "")
set Printouts to text returned of (display dialog "Number of Printouts?" default answer "0")
-- update the total number of prints
set TotalPrints to (TotalPrints + Printouts)
-- concatenation of text variables
set NewLine to JobNo & " " & JobDescription & return & "Prints: " & Printouts & return & "Total Printouts: " & TotalPrints & return
-- target file variable
set thelogfile to ((path to desktop as Unicode text) & "Xerox log.txt")
-- script that writes to the file
set XLF to open for access thelogfile with write permission
try
write return & date_header & return to XLF starting at eof -- NB. Start first write to just-opened file at eof.
write NewLine to XLF
close access XLF
on error e
close access XLF
display dialog "There was an error: " & e
end try
-- Macscripter contributors - Thanks to All.
-- Adam Bell - "open for access" code
-- Stefan K - changing from rtf file to txt file for opening post AS write
-- Nigel Garvey - for Date String coercion and "starting at eof" code to apend to the text file
Note: When the script is run from the “scripts” menu, the global total prints value is not retained. Maybe this is a panther issue. (10.3). Script saved as an App works fine. I’ll work from the applet.