{"id":136,"date":"2009-02-24T08:00:37","date_gmt":"2009-02-24T12:00:37","guid":{"rendered":"http:\/\/www.schollnick.net\/wordpress\/?page_id=136"},"modified":"2009-05-19T14:35:43","modified_gmt":"2009-05-19T18:35:43","slug":"short-applescripts","status":"publish","type":"page","link":"http:\/\/www.schollnick.net\/wordpress\/programming\/short-applescripts\/","title":{"rendered":"Short Applescripts"},"content":{"rendered":"<p><span style=\"font-weight: normal;\">Many of the following scripts are based on or are from the <\/span><a href=\"http:\/\/images.apple.com\/server\/macosx\/docs\/Leopard_Security_Config_2nd_Ed.pdf\"><span style=\"font-weight: normal;\">Apple Leopard Security Configuration, 2nd Edition<\/span><\/a><span style=\"font-weight: normal;\">, guide&#8230; \u00c2\u00a0These are some of the scripts that I have found useful at work, and for quick and easy use from ARD (Apple Remote Desktop). \u00c2\u00a0If 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.<\/span><\/p>\n<div>Others are from posts to Internet Applescript forums, and other sources that I have adapted for my own use. \u00c2\u00a0Feel free to grab anything you would like from here, but please let me know of any updates or improvements&#8230;<\/div>\n<hr \/>\n<p>Mounting a network drive<\/p>\n<p><code>set sfiles to \"smb:\/\/username@fileserver\"<br \/>\ntell application \"Finder\"<br \/>\nactivate      mount volume sfiles<br \/>\nend tell<\/code><\/p>\n<hr \/>\n<p>Hide a Time Machine Drive<\/p>\n<p><code>quit application \"Finder\"<br \/>\ntell application \"System Events\" to<br \/>\nset visible of disk \"TimeMachine Data\" to false<br \/>\ndelay 1<br \/>\nlaunch application \"Finder\"<br \/>\n<\/code><\/p>\n<hr \/>\n<p>This bit of Applescript adds a item to the login items:<\/p>\n<p><code>tell application \"System Events\" to make new login item with properties<br \/>\n{ path: \"\/path\/to\/YourApp.app\", hidden:false } at end<br \/>\n<\/code><br \/>\nAnd the equivalent python code:<\/p>\n<p><code>from appscript import *<br \/>\napp(\"System Events\").login_items.end.make(<br \/>\nnew=k.login_item,<br \/>\nwith_properties={<br \/>\nk.hidden: False,<br \/>\nk.name: \"UpLibJanitor\",<br \/>\nk.path:\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0\u00c2\u00a0 \"\/Applications\/Utilities\/UpLibJanitor.app\"<br \/>\n}<br \/>\n)<\/code><\/p>\n<p>or&#8230;<\/p>\n<p><code>from appscript import *<br \/>\na = app(\"System Events\")<br \/>\na.login_items()<br \/>\na.login_items.end.make(new=k.login_item,<br \/>\nwith_properties={k.path:\"\/Applications\/TextEdit.app\", k.hidden:True})<br \/>\n<\/code><br \/>\n(Adapted and translated from the AppleScript here:<br \/>\n<a href=\"http:\/\/macstuff.beachdogs.org\/blog\/?p=30\">http:\/\/macstuff.beachdogs.org\/blog\/?p=30<\/a><\/p>\n<hr \/>\n<p>Reset Safari Homepage via Applescript<\/p>\n<p><code>--reset the value of the Home Page<br \/>\nset HomePage to \"http:\/\/www.google.com.au\"<br \/>\n-- 'path to' is in the Standard Additions dictionary<br \/>\nset pListpath to ((path to preferences from user domain) as Unicode text) &amp; \"com.apple.internetconfigpriv.plist\"<\/code><\/p>\n<p><code> <\/code><\/p>\n<p><code><span style=\"font-weight: normal;\">tell application \"System Events\"<br \/>\n-- if the plist item does not contain the text<br \/>\nif (value of property list item \"WWWHomePage\" of property list file pListpath) is not HomePage then<br \/>\n-- set the item to the text<br \/>\nset (value of property list item \"WWWHomePage\" of property list file pListpath) to HomePage<br \/>\n-- if Safari is running, quit it<br \/>\nif (name of every process) contains \"Safari\" then tell application \"Safari\" to quit<br \/>\nend if<br \/>\n-- whatever happens start Safari<br \/>\ntell application \"Safari\" to launch<br \/>\nend tell<br \/>\n<\/span><\/code><\/p>\n<hr \/>\n<p>Set System Preferences to disable screen saver<\/p>\n<p><code>tell application \"System Preferences\"<br \/>\nactivate<br \/>\nset current pane to pane \"com.apple.preference.security\"<br \/>\ntell application \"System Events\"<br \/>\nclick checkbox 0 of window 0 of process \"System Preferences\"<br \/>\nend tell<br \/>\nquit<br \/>\nend tell<br \/>\n<\/code><\/p>\n<hr \/>\n<p><span style=\"font-weight: normal;\">To Force the system to download &amp; install all updates from the Apple Software Update Server<\/span><\/p>\n<p style=\"padding-left: 30px; \"><code><span style=\"font-weight: normal;\"># Updating from Internet Software Update Server<br \/>\n# -----------------------------------<br \/>\n# Download and install software updates.<br \/>\nsoftwareupdate --download --all --install<\/span><\/code><\/p>\n<hr \/>\n<p><span style=\"font-weight: normal;\">To Force the system to download &amp; install all updates from the Apple Software Update Server, on a package by package basis.<\/span>\n<\/p>\n<p style=\"padding-left: 30px; \"><code><span style=\"font-weight: normal;\"># Updating from Internet Software Update Server<br \/>\n# Updating Manually from Installer Packages<br \/>\n# -----------------------------------<br \/>\n# Download software updates.<br \/>\nsoftwareupdate --download --all<br \/>\n# Install software updates.<br \/>\ninstaller -pkg $Package_Path -target \/Volumes\/$Target_Volume<\/span><\/code><\/p>\n<hr \/>\n<p><span style=\"font-weight: normal;\">Running Disk Utility to Repair Disk Permissions via command line<\/span>\n<\/p>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\"># -----------------------------------<br \/>\n# Repair disk permissions.<br \/>\ndiskutil repairPermissions \/Volumes\/$Target_Boot_Drive<br \/>\n<\/span><\/code>\n<\/p>\n<p style=\"padding-left: 30px;\">\n<hr \/>\n<p><span style=\"font-weight: normal;\">Add Access Warnings to the Login Window<\/span><\/p>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\">sudo defaults write \/Library\/Preferences\/com.apple.loginwindow\u00c2\u00a0LoginwindowText \u00e2\u20ac\u0153Warning Text\u00e2\u20ac\u009d<br \/>\n<\/span><\/code><\/p>\n<hr \/>\n<p><span style=\"font-weight: normal;\">Hide Users below User # 500<\/span>\n<\/p>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\">sudo defaults write \/Library\/Preferences\/com.apple.loginwindow Hide500Users<br \/>\n-bool YES<\/span><\/code>\n<\/p>\n<p style=\"padding-left: 30px;\">\n<hr \/>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\"># Change an account\u00e2\u20ac\u2122s password.<br \/>\n# Don\u00e2\u20ac\u2122t use the following command on a computer that could possibly have<br \/>\n# other users logged in simultaneously.<\/span><\/code>\n<\/p>\n<p style=\"padding-left: 30px;\">\n<p><code><span style=\"font-weight: normal;\">sudo dscl . passwd \/Users\/$User_name $Oldpass $Newpass<\/span><\/code><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: -webkit-monospace;\"><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/span><\/p>\n<hr \/>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\">Remove Password hints.<\/span><\/code><\/p>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\"><br \/>\ndefaults write \/Library\/Preferences\/com.apple.loginwindow RetriesUntilHint -int 0<br \/>\n<\/span><\/code><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: -webkit-monospace;\"><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/span><\/p>\n<hr \/>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\"># Set the login options to display name and password in the login window.<br \/>\ndefaults write \/Library\/Preferences\/com.apple.loginwindow SHOWFULLNAME -bool yes<br \/>\n<\/span><\/code><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: -webkit-monospace;\"><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/span><\/p>\n<hr \/>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\"># Disable Show the Restart, Sleep, and ShutDown Buttons.<br \/>\ndefaults write \/Library\/Preferences\/com.apple.loginwindow PowerOffDisable -bool yes<br \/>\n<\/span><\/code><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: -webkit-monospace;\"><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/span><\/p>\n<hr \/>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\">Disable fast user switching.<br \/>\ndefaults write \/Library\/Preferences\/.GlobalPreferences MultipleSessionEnabled -bool NO<br \/>\n<\/span><\/code>\n<\/p>\n<p style=\"padding-left: 30px;\">\n<hr \/>\n<p><span style=\"font-weight: normal;\">Disable Recent Applications<\/span><\/p>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\"><br \/>\n# -----------------------------<br \/>\n# Disable display of recent applications.<br \/>\ndefaults write com.apple.recentitems Applications -dict MaxAmount 0<br \/>\n<\/span><\/code>\n<\/p>\n<p style=\"padding-left: 30px;\">\n<hr \/>\n<p><span style=\"font-weight: normal;\">Securing Desktop &amp; Screen Saver Preferences<\/span><\/p>\n<p style=\"padding-left: 30px;\"><code><span style=\"font-weight: normal;\"># Set idle time for screen saver.\u00c2\u00a0XX\u00c2\u00a0is the idle time in seconds.<br \/>\ndefaults -currentHost write com.apple.screensaver idleTime -int\u00c2\u00a0XX<br \/>\n<\/span><\/code><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: -webkit-monospace;\"><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/span>\n<\/p>\n<p style=\"padding-left: 30px;\">\n<p><span style=\"font-family: -webkit-monospace;\"><span style=\"font-weight: normal;\"><br \/>\n<\/span><\/span><\/p>\n<hr \/>\n<div>\n<h2>Free Disk Space<\/h2>\n<p>Over at\u00c2\u00a0<a href=\"http:\/\/www.macintouch.com\/readerreports\/helpplease\/index.html#d19may2009\">Macintouch<\/a>, a user has contributed an Applescript that will display when the amount of freespace on the drive, drops below a preset limit.<\/p>\n<pre>tell application 'Finder'<\/pre>\n<pre>-- sets up variables<\/pre>\n<pre>set theSize to capacity of startup disk -- total size of disk<\/pre>\n<pre>set freeSpace to free space of startup disk -- amount of free space<\/pre>\n<pre>-- changes size in bytes to size in MB and rounds it off<\/pre>\n<pre>set theSize to round (theSize \/ (1028 * 1028))<\/pre>\n<pre>set freeSpace to round (freeSpace \/ (1028 * 1028))<\/pre>\n<pre>-- calculates the percentage of free disk space<\/pre>\n<pre>set percentFree to round (freeSpace \/ theSize) * 100<\/pre>\n<pre>if percentFree  10 then<\/pre>\n<pre>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<\/pre>\n<pre>else if percentFree  20 then<\/pre>\n<pre>display dialog 'You have less then 20% free disk space. Your computer is preparing to explode, so please prepare to run away.' buttons {'Hmmm\u00e2\u20ac\u00a6'} with icon caution<\/pre>\n<pre>else if percentFree  30 then<\/pre>\n<pre>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<\/pre>\n<pre>(*<\/pre>\n<pre>else<\/pre>\n<pre>display dialog 'You have ' &amp; percentFree &amp; '% free space left. Keep working until I tell you to stop.'<\/pre>\n<pre>*)<\/pre>\n<pre>end if<\/pre>\n<pre>end tell<\/pre>\n<p>[&#8230;] 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&#8217;ve reached an appropriate level of &#8216;explosiveness.&#8217; If you&#8217;ve not reached any of the percentages, the user won&#8217;t see anything at all.&#8221;<\/p><\/div>\n<p><span style=\"font-weight: normal;\">Next batch coming soon!<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many of the following scripts are based on or are from the Apple Leopard Security Configuration, 2nd Edition, guide&#8230; \u00c2\u00a0These are some of the scripts that I have found useful at work, and for quick and easy use from ARD (Apple Remote Desktop). \u00c2\u00a0If you are looking to strengthen the security of your Macintosh, I <a class=\"read-more\" href=\"http:\/\/www.schollnick.net\/wordpress\/programming\/short-applescripts\/\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":69,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"_links":{"self":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/pages\/136"}],"collection":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/comments?post=136"}],"version-history":[{"count":0,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/pages\/136\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/pages\/69"}],"wp:attachment":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/media?parent=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- WP Super Cache is installed but broken. The constant WPCACHEHOME must be set in the file wp-config.php and point at the WP Super Cache plugin directory. -->