Short Applescripts

Many of the following scripts are based on or are from the Apple Leopard Security Configuration, 2nd Edition, guide…  These are some of the scripts that I have found useful at work, and for quick and easy use from ARD (Apple Remote Desktop).  If you are looking to strengthen the security of your Macintosh, I highly recommend downloading this FREE Security guide and using it as a basis of your hardening of the system.

Others are from posts to Internet Applescript forums, and other sources that I have adapted for my own use.  Feel free to grab anything you would like from here, but please let me know of any updates or improvements…

Mounting a network drive

set sfiles to "smb://[email protected]"
tell application "Finder"
activate mount volume sfiles
end tell


Hide a Time Machine Drive

quit application "Finder"
tell application "System Events" to
set visible of disk "TimeMachine Data" to false
delay 1
launch application "Finder"


This bit of Applescript adds a item to the login items:

tell application "System Events" to make new login item with properties
{ path: "/path/to/YourApp.app", hidden:false } at end

And the equivalent python code:

from appscript import *
app("System Events").login_items.end.make(
new=k.login_item,
with_properties={
k.hidden: False,
k.name: "UpLibJanitor",
k.path:         "/Applications/Utilities/UpLibJanitor.app"
}
)

or…

from appscript import *
a = app("System Events")
a.login_items()
a.login_items.end.make(new=k.login_item,
with_properties={k.path:"/Applications/TextEdit.app", k.hidden:True})

(Adapted and translated from the AppleScript here:
http://macstuff.beachdogs.org/blog/?p=30


Reset Safari Homepage via Applescript

--reset the value of the Home Page
set HomePage to "http://www.google.com.au"
-- 'path to' is in the Standard Additions dictionary
set pListpath to ((path to preferences from user domain) as Unicode text) & "com.apple.internetconfigpriv.plist"

tell application "System Events"
-- if the plist item does not contain the text
if (value of property list item "WWWHomePage" of property list file pListpath) is not HomePage then
-- set the item to the text
set (value of property list item "WWWHomePage" of property list file pListpath) to HomePage
-- if Safari is running, quit it
if (name of every process) contains "Safari" then tell application "Safari" to quit
end if
-- whatever happens start Safari
tell application "Safari" to launch
end tell


Set System Preferences to disable screen saver

tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.security"
tell application "System Events"
click checkbox 0 of window 0 of process "System Preferences"
end tell
quit
end tell


To Force the system to download & install all updates from the Apple Software Update Server

# Updating from Internet Software Update Server
# -----------------------------------
# Download and install software updates.
softwareupdate --download --all --install


To Force the system to download & install all updates from the Apple Software Update Server, on a package by package basis.

# Updating from Internet Software Update Server
# Updating Manually from Installer Packages
# -----------------------------------
# Download software updates.
softwareupdate --download --all
# Install software updates.
installer -pkg $Package_Path -target /Volumes/$Target_Volume


Running Disk Utility to Repair Disk Permissions via command line

# -----------------------------------
# Repair disk permissions.
diskutil repairPermissions /Volumes/$Target_Boot_Drive


Add Access Warnings to the Login Window

sudo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText “Warning Text”


Hide Users below User # 500

sudo defaults write /Library/Preferences/com.apple.loginwindow Hide500Users
-bool YES


# Change an account’s password.
# Don’t use the following command on a computer that could possibly have
# other users logged in simultaneously.

sudo dscl . passwd /Users/$User_name $Oldpass $Newpass



Remove Password hints.


defaults write /Library/Preferences/com.apple.loginwindow RetriesUntilHint -int 0



# Set the login options to display name and password in the login window.
defaults write /Library/Preferences/com.apple.loginwindow SHOWFULLNAME -bool yes



# Disable Show the Restart, Sleep, and ShutDown Buttons.
defaults write /Library/Preferences/com.apple.loginwindow PowerOffDisable -bool yes



Disable fast user switching.
defaults write /Library/Preferences/.GlobalPreferences MultipleSessionEnabled -bool NO


Disable Recent Applications


# -----------------------------
# Disable display of recent applications.
defaults write com.apple.recentitems Applications -dict MaxAmount 0


Securing Desktop & Screen Saver Preferences

# Set idle time for screen saver. XX is the idle time in seconds.
defaults -currentHost write com.apple.screensaver idleTime -int XX




Free Disk Space

Over at Macintouch, a user has contributed an Applescript that will display when the amount of freespace on the drive, drops below a preset limit.

tell application 'Finder'
-- sets up variables
set theSize to capacity of startup disk -- total size of disk
set freeSpace to free space of startup disk -- amount of free space
-- changes size in bytes to size in MB and rounds it off
set theSize to round (theSize / (1028 * 1028))
set freeSpace to round (freeSpace / (1028 * 1028))
-- calculates the percentage of free disk space
set percentFree to round (freeSpace / theSize) * 100
if percentFree  10 then
display dialog 'You have less then 10% free disk space. Your computer will explode momentarily, so please run away now.' buttons {'Run!'} with icon stop
else if percentFree  20 then
display dialog 'You have less then 20% free disk space. Your computer is preparing to explode, so please prepare to run away.' buttons {'Hmmm…'} with icon caution
else if percentFree  30 then
display dialog 'You have less then 30% free disk space. Please tell someone about this as your computer is due to explode sometime in the near future.' buttons {'OK'} with icon note
(*
else
display dialog 'You have ' & percentFree & '% free space left. Keep working until I tell you to stop.'
*)
end if
end tell

[…] Save the script somewhere, then save it again as an application rather than a script (making sure to uncheck all the options in the Save As dialog). All you have to do is make the application a startup item (in the Login Items tab of the Accounts section of the System Preferences). Every time the machine is started the app will run and warn you if you’ve reached an appropriate level of ‘explosiveness.’ If you’ve not reached any of the percentages, the user won’t see anything at all.”

Next batch coming soon!