Please note that the scripts below are for information only, you must test them before and adapt for your own usage.

Get password from keychain

Insert a new password on the Mac OsX Keychain application with your admin password. This function permit to not have the password in clear, in the script.


on getPassword(keychainItemName)
	local password
	set password to do shell script ("/usr/bin/security 2>&1 >/dev/null find-generic-password -gl " & quoted form of keychainItemName & " | cut -c 11-99 | sed 's/\"//g'")
	if password contains "could not be found in the keychain" or password as string is equal to "" then
		display alert "Password not found in the keychain" message "Certain tasks in this script need the administrator password to work.
You must create a new password in the OS X Keychain with a custom name, and set it with your administrator password, then edit this script." as critical
		error "Password could not be found in the keychain."
	else
		return password
	end if
end getPassword

set my_password to getPassword("PassKeychainName")

Run script in AppleScript


script DisableScreenSaverPassword
	# Your code here
end script

run DisableScreenSaverPassword

Grow Notifications

Download Growl Notify here : http://growl.info/downloads


try
	do shell script ("heure=$(date +'%H:%M:%S'); /usr/local/bin/growlnotify --appIcon '/Applications/BlueSense.app' --name BlueSense --noteName 'Device detected' -s --message \"$heure Welcome back to your desktop.\"")
end try



Device Detected AppleScripts exemples

Disable ScreenSaver Password

This part checks whether the screen saver is active or not, and enter the password if so. It’s wake the before.

When the screensaver is active with password set, windows, menus, keyboard are freezed.
If the script deactivate the password without entered the password before, the system will stay freezed and you will have to hard reboot your computer.
It's for this reason that is why this script is larger than others, It checks that the screen saver is active, and enters the password for you..

Display Sleep : If you want to wake and sleep the display, you can compile SleepDisplay, a Mac OS X Program to manage the display sleep : https://github.com/kimhunter/SleepDisplay/
If you just want to wake-up the screen, use the caffeinate command as below.


script DisableScreenSaverPassword
try
	set my_password to getPassword(KeychainPasswordName)

	-- Wake up the display
	if ScreenIsLocked() then
		# do shell script ("/usr/local/bin/SleepDisplay -wake") -- method 1 with SleepDisplay https://github.com/kimhunter/SleepDisplay
		do shell script ("/usr/bin/caffeinate -u -t 1") -- method 2 with caffeinate
		delay 3
	end if

	-- Enter the Screensaver Password
	tell application "System Events" to key code 53 using command down
	delay 2

	if (my ScreenSaverRunning()) or (my ScreenIsLocked()) then
		tell application "System Events"
			keystroke "a" using command down -- select all
			keystroke my_password --enter password
			delay 1
			key code 52 -- enter
		end tell
	end if

	delay 2

	-- Disable the password for the Screen Saver
	if not (my ScreenSaverRunning()) then
		-- method 1
		tell application "System Events" to set properties of security preferences to {require password to wake:false}

		--method 2
		(*
		tell application "System Events"
			tell security preferences
				activate
				set properties to {require password to wake:false}
			end tell
		end tell
		*)

		-- method 3
		(*
		do shell script ("defaults write ~/Library/Preferences/com.apple.screensaver.plist askForPassword -int 0")
		do shell script ("defaults write ~/Library/Preferences/com.apple.screensaver.plist askForPasswordDelay -float 0")
		*)
	end if

	-- Reactivate the Screen Saver at night between 20 PM and 8 AM
	if RunLaunchScreenSaverAfter8PM as boolean then
		set hour to hours of (current date)
		if hour ≥ 20 and hour ≤ 23 or hour ≥ 0 and hour < 8 then
			tell application id "com.apple.ScreenSaver.Engine" to launch
		end if
	end if

on error errString number errorNumber
	log (errString)
end try
end script

Enable auto login

Re-enable Mac OS X Automatic login with your username.


script EnableAutomaticLogin
try
	set my_password to getPassword(KeychainPasswordName)
	set my_username to getUsername()
	do shell script ("/usr/bin/defaults write /Library/Preferences/com.apple.loginwindow.plist autoLoginUser " & quoted form of my_username) user name my_username password my_password with administrator privileges
on error errString number errorNumber
	log (errString)
end try
end script

Set Skype Status to ONLINE

Change the status of Skype to ONLINE (if AWAY and running).


script SkypeStatusOnline
try
	tell application "System Events"
		if (exists process "Skype") then
			tell application "Skype"
				send command "GET USERSTATUS" script name "BlueSense"
				set lestatut to the result
				if lestatut is "USERSTATUS AWAY" then
					send command "SET USERSTATUS ONLINE" script name "BlueSense"
				end if
			end tell
		end if
	end tell
on error errString number errorNumber
	log (errString)
end try
end script

Play Spotify player (Mac)

Play Spotify Mac player.


script SpotifyPlayPause
try
	tell application "System Events"
		activate
		set runningApp to processes whose bundle identifier is "com.spotify.client"
	end tell

	if runningApp is not {} then
		tell application "Spotify" to play
	end if
on error errString number errorNumber
	log (errString)
end try
end script



Device Signal Lost AppleScripts exemples

Enable ScreenSaver Password

Launch the screensaver and enable the password to protect your Mac.


script EnableScreenSaverPassword
	try
		-- method 1
		tell application "System Events"
			tell security preferences
				activate
				set properties to {require password to wake:true}
			end tell
			tell application id "com.apple.ScreenSaver.Engine" to launch
		end tell
		-- method 2 (change user defaults but do not prompt for the password)
		(*
		do shell script ("/usr/bin/defaults write com.apple.screensaver askForPassword -int 1")
		do shell script ("/usr/bin/defaults write com.apple.screensaver askForPasswordDelay -int 5")
		-- Launch the  ScreenSaver
		tell application id "com.apple.ScreenSaver.Engine" to launch
		*)
	on error errString number errorNumber
		log (errString)
	end try
end script

Disconnect session

This script qui automatically the current user session on Mac OS X.

Note that if the app loose your device signal, you will be disconnected…


script DisconnectSession
    try
        do shell script ("/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -suspend")
    end try
end script

Disable auto login

Disable the Mac OS X automatic login session and prompt for the password next time the computer will start.


script DisableAutomaticLogin
	try
		set my_password to getPassword(KeychainPasswordName)
		set my_username to getUsername()
		do shell script ("/usr/bin/defaults delete /Library/Preferences/com.apple.loginwindow.plist autoLoginUser") user name my_username password my_password with administrator privileges
	on error errString number errorNumber
		log (errString)
	end try
end script

Set Skype Status to AWAY

Change the status of Skype to AWAY (if ONLINE and running).


script SkypeStatusAway
try
	tell application "System Events"
		if (exists process "Skype") then
			tell application "Skype"
				send command "GET USERSTATUS" script name "BlueSense"
				set lestatut to the result
				if lestatut is "USERSTATUS ONLINE" then
					send command "SET USERSTATUS AWAY" script name "BlueSense"
				end if
			end tell
		end if
	end tell
on error errString number errorNumber
	log (errString)
end try
end script

Set Skype Status to AWAY

Change the status of Skype to AWAY (if ONLINE and running).


script SpotifyPlayPause
try
	tell application "System Events"
		activate
		set runningApp to processes whose bundle identifier is "com.spotify.client"
	end tell

	if runningApp is not {} then
		tell application "Spotify" to pause
	end if
on error errString number errorNumber
	log (errString)
end try
end script

GitHub Repo

You can find the AppleScripts examples below in the GitHub Repository: https://github.com/atika/bluesense