Perform a run only script once

Is there a way I can get a run only script to perform just once

Hello.

You can save a property, if you can’t save a regular property with a run-only script then see next post.

To perform only once forever, after each boot or user session?

To perform once forever you could use McUsr’s solution.

To run a script once after each boot you could store the script in the startup items folder in /Library or when the script needs to be launched by an user you can use an file or property and store the boot date of the computer in there. Then when the script starts you compare these boot date. You can get the boot date by using sysctl:

do shell script "sysctl -n kern.boottime | sed 's/.*} //'"

The easiest way would be puuting your script in the user’s login items but if you want the script be launched at any time and still no more than once each user session you could do the following: The WindowServer will be restarted when an user logs out. So to determine if the user has logged out and in again between runs, the script can take a look if the pid of the WindowServer has changed. You should store this data in an file in path to temporary items (read:not from user domain but system domain) because these items will be removed after reboot. To get the PID of the WindowServer you can use:

do shell script "ps -Ac -o 'pid,comm' | grep WindowServer | awk '{print $1}'"

Hello.

Here is a way to get the username, so that you can run the script only once per user, it returns the 8 first letters of the short user name (for some reason), but I think that should be enough for most cases!

do shell script "/usr/bin/who am i | /usr/bin/sed -n 's/\\([^ ]*\\)[ ].*/\\1/p'"

Why not the easy way?


short user name of (system info)

or


tell application "System Events" to get name of current user

or even if a shell script is “mandatory”


do shell script "whoami"

Hello.

Here is one more that I found when I looked for the right command, but the AppleScript one, would be the best one. (I hardly think you can manage to change the user name that AppleScript has, so that is the one I’d trust, if trust is an issue.)

do shell script "echo $USER"

Thanks everyone for the input.

What I am trying to achieve is to write an installer script that would only perform its task once. It would not be possible to have a load script command or a write to file to store values since the installer needs to work on foreign machines. It has to change its values within itself,

Mcusrll

Seems to be the way forward but I have no idea what property I need to write that will cause the script to run once.

Thanks again for the interest and help. I am very flakey on this area of programming

If you really want to run the script only once, use this code and save the installer script as application.
Properties in applets are persistent


property firstRun : true

if firstRun is false then
	quit
else
	set firstRun to false
end if
installThings()

but in this case you could also destroy the installer (script) at the end

Perfect as always StefanK. Thank you

Hello.

For prosperity, this one returns the machine UUID, if something is to be run once per machine.

set UUID to (do shell script "system_profiler -detailLevel basic 2>/dev/null |grep \"Hardware UUID\" |sed -n 's/[         ]*Hardware UUID:[ ][ ]*//p'")

another nitpicking: “system_profiler -detailLevel basic 2>/dev/null” is VERY expensive because it lists tons of information.
“system_profiler SPHardwareDataType” lists only the parameters in the Hardware Overview window of the GUI

Then you only need to check if certain, required, files are installed. I mean an installer needs to be able to run every time and it checks if the software/data is already installed or not and only (over)write to the drive that is needed.

Thanks DJ.

I am working at an installer (type of!) script for a product(using filemaker and applescript) I am working on. I am finding a way of getting the product registered and locked without the use of passwords or serial numbers. I will post results eventually if anyone is interested.

Here is a faster way to get the UUID:

set UUID to (do shell script " ioreg -rd1 -c IOPlatformExpertDevice | sed -n '/UUID/ s/[ ][ ]*\"IOPlatformUUID\" = \"\\([^\"][^\"]*\\)\".*/\\1/p'")