Is it possible to create a value that increments or decrements over script reboot?
I know variable values vaporize on reboot so that’s out. Property values get preserved but I cannot create a script that would make the property assume a new value and then reflect this new value on reboot.
Is there a way to do this in AppleScript? I need some :idea:
i will try your idea but am not sure if the new value will be reflected over reboot. i think i have tried it before but probably haphazardly. will do it again.
I just analyzed your suggestion and have concluded it will not work for what I want to do.
What I really want is for a way to see how many times I have used my script. I can’t keep on incrementing the property value and then recompiling the script.
In summary, what I want is a built-in way to increment the property value whenever the script is used and then have the new value inserted into the property so that on reboot a new value is set for that property.
Is that possible? Or perhaps I did not really grasp what you suggested.
just for further clarification because may be i was not so clear in my intention.
what i really want to do is have a way to know how many times a script has been used by other users…not by me…that’s why i cannot change the value before closing the script and then reboot.
there is no way a script can tell who is running it but the current user. Greg, posted a script that increments the value of a property. The value of the property is saved within the script. If you read carefully, he said that when you recompile the script the value of the property is reinitialized. If you want to find out more about how properties are saved, you can read AppleScriptLanguageGuide.pdf…
There are many workarounds to various forms of this idea. Note that a script recards the value of every value. These values are retained unless the use declares the variables as ‘local’. You should do some experimenting as you probably are. This is good. I have found that users who can’t explain themselves clearly are probably experimenting.
This isn’t very efficient but this script will log the short user name of the person who is running the script and increment a property with that person’s name and the number of times they’ve run the script. If the user is the person who wrote the script (modify the “johndoe” line) then the app will give the stats of who has run the script & how many times and offer to reset the stats:
Again, opening and recompiling the script will cause the property to lose its value.
just to let you know…i have given up…am defeated and am giving up any new experiment on this issue.
with jon’s and greggo’s codes, and other codes i tried…yes…the values could change but not over reboot.
sorry to bother all of you…was just hoping i could find a solution using AppleScript. i guess i have to find a solution some other way…via commercial apps.
many thanks again for your efforts.
more power to macscripter.bbs and see you again in some future sessions.
How about writing a tiny text file that stores your info as a log. The following will save a small file in the preferences folder of the HD library. It should be accessible to all users and if the data stored is kept to minimal size it should be quick and transparent to the user.
on run
set adminUser to "jobu" --> The user name of the administrator
set myUserLog to "Macintosh HD:Library:Preferences:myUserLog" as file specification
set currentUser to system attribute "USER"
(* Import current log *)
try
set myLogContents to read myUserLog using delimiter "|"
on error
set myLogContents to {}
end try
(* Update the log data *)
set {tempLogContents, logDelim, userNotFound, adminLogContents} to {"", "", true, {}}
repeat with tempLogRecord in myLogContents
set theDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set {tempUser, tempSess} to text items of (tempLogRecord as string)
set AppleScript's text item delimiters to theDelim
if (tempUser) is (currentUser) then
set {tempSess, userNotFound} to {(tempSess + 1), false}
end if
set tempLogContents to (tempLogContents & logDelim & tempUser & ":" & tempSess) as string
set adminLogContents to adminLogContents & ((tempUser & " (" & tempSess & ")") as string)
set logDelim to "|"
end repeat
(* Add new user to log, if necessary *)
if userNotFound is true then
set tempLogContents to (tempLogContents & logDelim & currentUser & ":" & 1) as string
set adminLogContents to adminLogContents & ((currentUser & " (1)") as string)
end if
(* Write the log *)
try
open for access myUserLog with write permission
set eof of myUserLog to 0
write (tempLogContents as string) to myUserLog starting at eof
close access myUserLog
on error
try
close access myUserLog
end try
end try
(* Display the log contents to the admin *)
if (currentUser) is (adminUser) then
choose from list adminLogContents with prompt "My User Log:" with empty selection allowed
end if
end run
Hopefully this is along the lines of what you’re looking for.
many thanks for the suggestion. that’s exactly along the line that i need…a hidden file somewhere in the user’s hard disk to be updated each time the script is used. probably not absolutely fool-proof for many hackers or would be hackers but good enough for general purpose.
i saw some suggestion like this in AppleScript Studio Mailing List and was going to ask the guy how to do such thing until you came along.
will try your stuff and let you know. i think it should work.
You could make it an invisible file, and/or write some code to do some basic encryption of the file before writing it. That way the “prying eyes” may find it, but may not know what to make of it. As many topics here and elsewhere have discovered…no matter what you build, there’s always someone who can tear it down. :x
your codes worked like a charm after some modification to fit my script and inserting them.
now, all i have to do is to make that log file invisible or as you suggested, encrypt the it. i have done a little experiment on giving some cryptic file name but it’s not gonna last i know. like you said, someone, somewhere, always find a way to ruin what others have built.
that’s Homo sapiens’ nature. can we last in this world?
thanks again for your suggestions. will see you in BBS sometime again for sure.
script launchIncrementor
property fileLocation : -- choose it
on newTimeScript(number)
script
property times : number
end
end
on incrementTimes()
set oldNumber to getTimes()
store script (newTimeScript(oldNumber + 1)) in fileLocation replacing yes
end
on getTimes()
try
times of (load script fileLocation)
on error
0
end
end
end
this way the value is stored in an applescript context in the resource fork of the file you choose… if someone opens the file in script editor (the default) this context dissapears, but they only open an empty file…
for that matter, if you really wanna hide it well use the satimage osax to put it on the resource fork of a folder like ~/Documents, but this is less recommended because it is fragile for you and other apps, it is so well hidden you’ll forget it’s there, and this technique should not be over used because your folders data forks will grow…