Posts RSS Comments RSS 48 Posts and 155 Comments till now

Archive for the 'Applescript' Category

Quick Applescript to check your Mac for the Flashback infection

There is lots of media exposure on the possibility that multiple Macs may be infected by a trojan called “Flashback“. This involves a vulnerability in Java that malicious websites can exploit.

If you have Java on your Mac you need to make sure you have updated to the latest Java update from Apple to patch this issue. The easiest way is to just run Software Update on your Mac and get the latest updates from Apple. You can download them manually as well from Apple:

If you are concerned that you might already be infected F-Secure has some excellent instructions on how to find out here. The Applescript below will run the commands referenced in the F-Secure article to do a quick and dirty check. If everything comes back clean it will tell you. If it comes back as possibly infected it will direct you (or your user) to the website for more detailed information.

I’m posting it as a script file for download here:
Flashback Checker Script

Or you can copy the code from here and paste it into AppleScript Editor to run:

[codesyntax lang=”applescript”]
–see if this entry exists. If not an error will occur and be trapped
try
do shell script “defaults read /Applications/Safari.app/Contents/Info LSEnvironment”
–set this variable if this entry exists
set LSE to “true”
on error errmsg
–set this variable if the error contains the message “does not exist”
if errmsg contains “does not exist” then
set LSE to “false”
end if
end try

–search Firefox for infections
try
do shell script “defaults read /Applications/Firefox.app/Contents/Info LSEnvironment”
–set this variable if this entry exists
set FLSE to “true”
on error errmsg
–set this variable if the error contains the message “does not exist”
if errmsg contains “does not exist” then
set FLSE to “false”
end if
end try

–see if this entry exists. If not an error will occur and be trapped
try
do shell script “defaults read ~/.MacOSX/environment DYLD_INSERT_LIBRARIES”
set DLib to “true”
on error errmsg
–set this variable if the error contains the message “does not exist”
if errmsg contains “does not exist” then
set DLib to “false”
end if
end try

–if all variables are false then the machine isn’t infected
if LSE is “false” and DLib is “false” and FLSE is “false” then
display dialog “You are not infected with Flashback”

–if any variable is true the machine may be infected and needs closer inspection
else if LSE is “true” or DLib is “true” or FLSE is “true” then
display dialog “You may have the Flashback infection” & return & “Go to following URL for more information:” & return & return & “http://www.f-secure.com/v-descs/trojan-downloader_osx_flashback_i.shtml”
end if
[/codesyntax]

Viewing or hiding hidden files in the Finder using AppleScript

Mac OS X has a lot of files that are hidden from view in the Finder. They are hidden for a very good reason, which is most of them are system files that you don’t need to mess with if you want your computer to keep working well. However, there are occasions where it would nice to be able to view these files in the Finder.

This script lets you toggle the view on and off. So, you can turn it on, do what you need and then turn it off.

[codesyntax lang=”applescript”]
try
set toggleView to (do shell script “defaults read com.apple.Finder AppleShowAllFiles”)
on error
set toggleView to “NO”
end try

if toggleView is “NO” or toggleView is “0” then
set br to display dialog “Showing hidden files is disabled.” & return & “Would you like to enable it?” buttons {“Yes”, “No”} default button {“No”}
set theAnswer to the button returned of br
if theAnswer is “Yes” then
do shell script “defaults write com.apple.Finder AppleShowAllFiles YES”
do shell script “killall Finder”
display dialog “Showing hidden files has been enabled” buttons {“Ok”} giving up after 5
end if
else
if toggleView is “YES” or toggleView is “1” then
set br to display dialog “Showing hidden files is enabled.” & return & “Would you like to disable it?” buttons {“Yes”, “No”} default button {“Yes”}
set theAnswer to the button returned of br
if theAnswer is “Yes” then
do shell script “defaults write com.apple.Finder AppleShowAllFiles NO”
do shell script “killall Finder”
display dialog “Showing hidden files has been disabled” buttons {“Ok”} giving up after 5
end if
end if
end if
[/codesyntax]

Copy the code into AppleScript Editor and then save it out as an application. Double clicking on it will bring up a dialog box telling you the current state of viewing hidden files and gives you the option of switching it. Since you have to restart the Finder after you make this change you’ll see the Finder quit and restart.

Opening a FaceTime connection via AppleScript/Automator

The new FaceTime video chat beta is now available from Apple. It allows you to video chat with anyone using that app on any Mac running the service as well as an iPhone 4 or the latest iPod running iOS 4 or better. It’s extremely easy and I can see a lot of great uses for it.

One of the very cool things about it is that you can use a URL to initiate a FaceTime call. As described in this Mac OS X Hints article just use the following syntax:

  • facetime://appleid
  • facetime://email@address
  • facetime://phone# as a URL in Safari’s address bar.

Since you can use a URL that means you can use AppleScript to connect to a session or Automator to even schedule and initiate a call using iCal Alarms.

The only line you need is this:

[codesyntax lang=”applescript] open location “facetime://joe@emailaddress.com”[/codesyntax]

Obviously you can use any of the URL syntaxes listed above in place of the email address. Save it as a script/application/workflow and you’re ready to chat!

Check how much disk space is left using AppleScript

This is a fairly simple one but someone out there may need this. My boss recently had a weird problem where his disk was suddenly filling up. A reboot fixed it but we were having a hard time finding the process that was suddenly eating 15 GB of disk space with no warning. So, I came up with this script that just gets how much free space is left on his disk. We ran it as a cron job every 15 minutes so we could hopefully get some warning before things filled up and became unusable. The script displays a warning when the disk has 5% or less of free space. You can edit it for your own uses. It also has the ability to email the report to you. Comment out the section you don’t want to use. To have it send emails when you are not logged in use something like Lingon for a launchd item or Cronnix for a cron job. Save your script in the appropriate location and have the launchd/cron job call it using the “osascript” command. So, if the file was saved in the /Users/Shared folder the line would read:

osascript /Users/Shared/disk_check.scpt

[codesyntax lang=”applescript”]
set mysubject to “Disk Usage Report”
set myrecipient to “sysadmin@example.com”

–get the amount of free space
set dSize to (do shell script “df -h / | grep %”) as text

–pull out the percent used from the result
set theTotal to word 6 in dSize as number

–if the total used space is 95% or more put up a warning. Comment out if running the email section
if theTotal is greater than or equal to 95 then
display dialog “Your disk is ” & theTotal & “% full”
end if

–comment out this section if no email is desired.
set mybody to (“Free Space: ” & theTotal & “%”)
do shell script (“echo \”” & mybody & “\” | mail -s \”” & mysubject & “\” ” & myrecipient)
[/codesyntax]

Click here to download a copy: Disk Check script

Getting user login data from Macs using AppleScript

A lab we work with recently upgraded to 10.5 and found that the program they were running to get the user login data for their billing wouldn’t work in 10.5 and wasn’t going to be upgrading. In looking at the data it was grabbing it was clear the same data could be gotten using the last command in Terminal. You can see more about last at my post here. However, these folks were not computer-savvy and needed the information in a comma-delimited file. So, I came up with this script.

When you run the script it asks you which month you want the data for in a list. Select the month and it will generate a file on the desktop with that months data. You can save it out as an application, run it from Script Editor or run it as a launchd/cron job. You can customize the output by changing the line that begins with “write”. For example, replace the “,” with tab and create a tab-delimited file instead.

If you run this script logged in as a regular user you will only get the login information for that user. If you run it as an administrator you’ll get the login information for every account on the machine.

Click here to download the script file:
Monthly Login accounting

[codesyntax lang=”applescript” lines=”no”]
set the_months to {}
set the_total to 0
set the_time to “”
set the_computer to (computer name of (system info))

--get the raw login log information
set the_login to (do shell script “last”) as text

set the_count to the count of paragraphs of the_login

--get the list of all the months login data is available for
repeat with x from 1 to the_count
if paragraph x of the_login contains “console” then
set month_check to (word 4 of paragraph x of the_login) as text
if the_months does not contain month_check then
set end of the_months to month_check
end if
end if
end repeat
set selected_month to choose from list the_months with prompt “Select the month:”

--create the file to write data to
if selected_month is not false then
tell application “Finder”
set file_name to (the_computer & “-” & selected_month & “-” & (year of (current date)) & ” login stats.txt”)
if not (exists file (((path to desktop from user domain as text) & file_name))) then
set login_file to make file with properties {name:file_name, file type:”TEXT”} at (path to desktop from user domain)
else
display dialog “This stats file already exists. Do you want to overwrite it?” buttons {“Yes”, “No”} default button 2
set the_answer to the button returned of the result
if the_answer is “Yes” then
delete file ((path to desktop from user domain as text) & file_name)
set login_file to make file with properties {name:file_name, file type:”TEXT”} at (path to desktop from user domain)
end if
end if
end tell
set login_file to ((path to desktop from user domain) & file_name) as text
end if

--loop through the data and pull out the logins for the selected month
repeat with x from 1 to the_count
if paragraph x of the_login contains selected_month then
if paragraph x of the_login contains “console” then
set the_user to (word 1 of paragraph x of the_login)
set the_month to (word 4 of paragraph x of the_login) as text
set the_logmonth to MonthNumber(the_month)
set the_day to (word 5 of paragraph x of the_login) as text
if (the (count of characters of the_day) is less than 2) then
set the_day to (“0” & the_day)
end if
set the_year to yearCheck(selected_month, the_months)
set log_date to (the_logmonth & “/” & the_day & “/” & the_year)
set login_time to (word 6 of paragraph x of the_login & “:” & word 7 of paragraph x of the_login)
set logout_time to (word 8 of paragraph x of the_login & “:” & word 9 of paragraph x of the_login)

set old_delims to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to “(”
try
set the_time1 to text item 2 of paragraph x of the_login
set AppleScript’s text item delimiters to old_delims
set the_time to (characters 1 through 5 of the_time1) as text
on error
set AppleScript’s text item delimiters to old_delims
end try

–write all the date to the file
write (the_user & “,” & the_computer & “,” & log_date & “,” & login_time & “,” & log_date & “,” & logout_time & “,” & the_time & return) to file login_file starting at eof
end if
end if
end repeat

--if the list contains January see if the selected month comes before or after January. Adjust the year accordingly.
on yearCheck(selected_month, the_months)
if the_months contains “Jan” then
repeat with x from 1 to count of the_months
if item x of the_list contains selected_month then
set the_num to x
else
if item x of the_list contains “Jan” then
set jan_num to x
end if
end if
end repeat
set year_check to (the_num – jan_num)
if year_check is greater than 0 then
set the_year to ((year of (current date)) – 1)
else
set the_year to (year of (current date))
end if
else
set the_year to (year of (current date))
end if
return the_year
end yearCheck

on MonthNumber(the_month)
if the_month = “Jan” then
set the_month to “01”
else
if the_month = “Feb” then
set the_month to “02”
else
if the_month = “Mar” then
set the_month to “03”
else
if the_month = “Apr” then
set the_month to “04”
else
if the_month = “May” then
set the_month to “05”
else
if the_month = “Jun” then
set the_month to “06”
else
if the_month = “Jul” then
set the_month to “07”
else
if the_month = “Aug” then
set the_month to “08”
else
if the_month = “Sep” then
set the_month to “09”
else
if the_month = “Oct” then
set the_month to “10”
else
if the_month = “Nov” then
set the_month to “11”
else
if the_month = “Dec” then
set the_month to “12”
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
end if
return the_month as string
end MonthNumber
[/codesyntax]

Enabling clear text passwords in Snow Leopard with AppleScript

Update: It appears that clear text passwords for AFP connections only work when booted into 32 bit mode. I’ve updated the script to check for which kernel the user is booted into. If they are running 64 bit it asks them if they want to switch to 32 bit. If they say “Yes” then it makes the switch and reboots the machine for them.

A nice article explaining how to see if you are running in 32 or 64 bit mode is here at MacObserver.

There is an Apple Knowledge base article dealing with servers but with good information on switching kernels here.

The procedure for enabling clear text passwords for AFP connections is the same in Snow Leopard as it is in Leopard with one very critical difference. The details about how and why are already in my post on Leopard. If you want the background information you should check out that page. This post will only deal with the Snow Leopard-specific changes.

The big change for enabling clear text passwords for Snow Leopard is that the .plist file is now a binary. This is something Apple has been moving towards since 10.4 and there is a built-in utility that allows you to change the format back and forth to allow for easy editing called “plutil”. The full path to it is “/usr/bin/plutil”

The flag we need to be aware of in “plutil” is the “-convert” flag. There are two formats that we’ll use for this flag, “xml1” and “binary1”.

To convert the plist file to XML to allow editing we have to run the following command:
/usr/bin/plutil -convert xml1 /Users/joe/Library/Preferences/com.Apple.AppleShareClient.plist

This will convert the file to XML for editing. Now we will do the actual editing. This line is the same as in Leopard.
defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES

Now that we have edited the file we have to convert it back to binary form. So we use the “plutil” tool again with a different format:
/usr/bin/plutil -convert binary1 /Users/joe/Library/Preferences/com.Apple.AppleShareClient.plist

Now the preference file is converted back to binary and can be used by the AFP client.

Here is an updated version of the Leopard AppleScript for changing this setting.

If you would prefer to download a pre-complied script file click below:
Snow Leopard Clear Text Script

[codesyntax lang=”applescript” lines=”no”]
set afp_pref_path to ((POSIX path of (path to preferences from user domain)) & “com.Apple.AppleShareClient.plist”)
set OS_version to (do shell script “sw_vers -productVersion”)
set kernel_answer to “”

--check if the user is running 32 or 64 bit kernel.
if OS_version contains “10.6” then
set kernel_version to (do shell script “/usr/sbin/systemsetup -getkernelbootarchitecturesetting”)
if kernel_version contains “x86_64” then
set kernel_answer to button returned of (display dialog “You are currently running in 64 bit mode. Clear text passwords only work in 32 bit mode. Would you like to change to 32 bit mode? This will require a restart.” buttons {“Yes, change it and restart”, “No, just enable clear text”} default button 1)
end if
end if

try
set clearStatus to (do shell script “defaults read com.Apple.AppleShareClient afp_cleartext_allow”) as number
on error
--the first command will throw an error if the afp_cleartext_allow setting does not exist
--if there is an error we’ll assume that the setting isn’t there and set our variable to the disabled setting
set clearStatus to 0
end try
--a status of “1” means it’s enabled. So ask if they want to disable it
if clearStatus is 1 then
display dialog “Do you want to disable clear text passwords?” buttons {“Cancel”, “Disable”} default button 2
if the button returned of the result is “Disable” then
do shell script “/usr/bin/plutil -convert xml1 ” & afp_pref_path
do shell script “defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool NO”
do shell script “/usr/bin/plutil -convert binary1 ” & afp_pref_path
set clearStatus to (do shell script “defaults read com.Apple.AppleShareClient afp_cleartext_allow”) as number
--check to make sure the change really took effect
if clearStatus is 0 then
display dialog “Clear text passwords have been disabled” buttons {“OK”}
else
display dialog “There was an error disabling clear text passwords!” buttons {“OK”}
end if
end if
else
display dialog “Do you want to enable clear text passwords?” buttons {“Cancel”, “Enable”} default button 2
if the button returned of the result is “Enable” then
do shell script “/usr/bin/plutil -convert xml1 ” & afp_pref_path
do shell script “defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES”
do shell script “/usr/bin/plutil -convert binary1 ” & afp_pref_path
set clearStatus to (do shell script “defaults read com.Apple.AppleShareClient afp_cleartext_allow”) as number
--check to make sure the change really took effect
if clearStatus is 1 then
display dialog “Clear text passwords have been enabled” buttons {“OK”}
else
display dialog “There was an error enabling clear text passwords!” buttons {“OK”}
end if
end if
end if

if kernel_answer contains “Yes” then
do shell script “/usr/sbin/systemsetup -setkernelbootarchitecture i386” with administrator privileges
do shell script “/sbin/shutdown -r now” with administrator privileges
end if

[/codesyntax]

Recommended AppleScript Books for Beginners

I’ve got several AppleScripts on this site and I get lots of questions about things relating to AppleScript. One of the questions I hear is “Are there any good books about AppleScript, especially for beginners”. Well, the answer is yes and no. I’ll give a few recommendations here that I think will help most beginning AppleScripters and possibly some more advanced ones. There are lots of sites out there as well such a Macscripter.net that also have a wealth of information. I encourage anyone looking into AppleScript to use these sites as well as printed materials.

Having said that I know that I personally benefit from having books around. In some cases it just an easy way to quickly look something up. Most books have examples on how to use either a scripting addition or a vanilla AppleScript command. Having those examples makes it a lot easier for me to figure things out.

Apple Training Series: AppleScript 1-2-3




This book has replaced Danny Goodman’s fantastic but dated AppleScript Handbook (see below) as my recommended book for first time AppleScripters. The first author, Sal Soghoian, has been the AppleScript/Automater Product Manager at Apple for many years. I’ve been to lots of his talks and demos and he always has great examples of things you can do with AppleScript. His examples are fun and useful at the same time. The other author, Bill Cheesman, has been involved in AppleScripting from almost the very beginning and is extremely active in the AppleScript community.

This book assumes you have no knowledge of AppleScript or programming. It starts you out in the first chapter on the basics of writing scripts using AppleScript Editor with some cool one line scripts. From there he takes you through using dictionaries, working with the Finder and the file system.

After you get comfortable with working there you move on to actually learning some programming techniques such as looping and properties. However, Sal and Bill do it in such a conversational way you don’t even realized you’re learning actual programming.

This book contains a ton of useful scripts that can either be used right out of the page or, after doing the examples, can be easily configured for your own needs.

At the end of the book they cover a little bit concerning AppleScript Studio for those who want to move to the next level and create actual applications using only AppleScript as well as an introduction to Automator.

All in all this is an incredible book for beginning AppleScripters. The AppleScript community has been waiting a long time for Sal (and Bill) to write a book and it was obviously worth the wait.

 
Danny Goodman’s Applescript Handbook

Until Sal’s book came out this was my go-to book. Danny Goodman has been around about as long as the Mac and his knowledge as well as his writing style are unsurpassed. He lays out the basics of AppleScript first and gives tons of examples each time. He steps you through each of the examples and explains what each line is about and why it does what it does. I learn languages best by doing lots of examples and see what things do. This book excels at that. The big issue with the scripts in the book is that they have become dated and you may find that some don’t work anymore as AppleScript is always evolving.

Many of the examples are short so you don’t get bogged down in code while trying to understand everything. It’s also a fantastic reference book. Whether you’re trying to find out what a list is or how to create a new folder all you have to do is look here. If you can find a used or on-sale copy I still highly recommend it as a reference book and intro to scripting.

 

AppleScript: The Missing Manual


Applescript: The Missing Manual

Another in the “Missing Manual” series this is another great book for beginners. It’s full of great and useful scripting examples that you can use right out of the box. I started writing AppleScripts to do little things that I got tired of doing over and over. You’ll find lots of examples in this book of just that. And along the way you’ll find yourself thinking about how you can adjust those scripts to suit your needs. And THAT is the best way, in my opinion, to learn AppleScript.

The first three chapters of the book are all about getting your feet wet. It starts with showing you where things like Script Editor lives and the wide variety of scripts that already come with your Mac. You’ll open several of those existing scripts and edit them to change what they do. A great way to introduce scripting in my opinion. Change something, see what it does and go from there. The last chapter in that section shows you how to start creating scripts from scratch in Script Editor. You’ll cover things like creating dialog boxes and opening the AppleScript dictionaries of various applications to see what you can do with them.

The second section gets into more of the everyday aspects of AppleScript and what you can do with it. It covers topics such as manipulating text in Text Edit and Microsoft Word and how to move, select and delete files. People who work with graphics on a regular basis will like the sections on controlling Photoshop and iPhoto with AppleScript. I particularly liked the fact that he covers using the built-in Image Events to manipulate graphics. Image Events comes with your OS and lets you do very cool things with graphics without ever opening a program, such as determine color space, rotate the image or even convert it from one format to another.

This section also covers scripting iTunes and Quicktime but only briefly. With AppleScript you can access the features of Quicktime Pro without paying for the Pro license. It would have been nice to see more of that but the Quicktime section gets you started.

The Internet and Networking section has some useful scripts for Internet Connect and Airport but I think most people will find the Mail and iChat scripting section the most useful. The final chapter in this section covers database scripting, in particular making a small AppleScript database and scripting Filemaker Pro.

The third section of the book is labeled as the “Power Users” section but there are lots of things in there that the beginning scripter will find incredibly useful. Things like assigning scripts to folder actions so they run automatically will be immediately useful to many. And the section on running UNIX commands using AppleScript will open up a whole new world to many. There is a very brief chapter on using AppleScript Studio to create applications. It’s enough to get your started if you’re interested but otherwise you would be better off getting an actual AppleScript Studio book.

The most useful part of this section for any scripter has to be the Debugging section. Nothing is more frustrating when writing scripts, especially for new scripters, then getting error messages and having no idea what they mean. Script Editor does a great job of pointing out where the problem is but not in telling you how to fix it. This section covers in detail what many of those error messages mean and how to fix them. It also covers how to trap for errors that may occur and what to do in your script when they do.

 

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition (Learn Series)


This book is one of the more recent and takes things to the next level from the “Missing Manual” series. This book takes you into the meat of AppleScript and shows you how to do really great things like scripting all those command line tools you know are there but don’t want to open Terminal to use. You’ll even learn how to write scripts that can be executed on a remote Mac. This can be really handy when you need to re-start the Filemaker Pro database from across a campus or business. After you’ve gotten an understanding of the basics of AppleScript from the first two books this one will take you to the next level and get you writing more involved and exciting scripts.

These books are just a small sample of the things that are out there but I think these books will give anyone who is starting in AppleScript or wants a more complete understand the foundation they need. As I said, I like books that give lots of real world examples and all of these do exactly that and more.

Turning your Airport Card on and off

This is a simple tip I stumbled across a month or so ago. There were some bugs in Leopard wireless that wouldn’t let me connect to the encrypted wireless network at work after a machine was restarted. I found that turning the airport card on and off let me connect again. I hated having to remember this every time I restarted so I dug around and found that the command line tool networksetup can do it for me. Great!

This tool exists on Tiger machines in the Apple Remote Desktop client bundle. The path to it is:

/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Support/networksetup

Fortunately they very kindly included it in the build for Leopard. The path in Leopard is:

/usr/sbin/networksetup

So, in Leopard, to turn the Airport card off enter the following in Terminal:

/usr/sbin/networksetup -setairportpower off

To turn the card back on change “off” to “on”. If you’re running Tiger make sure to change the path so it points to the app correctly.

Put both of these commands in an AppleScript, save it as an application and add it to your Login items. Then, when the machine is rebooted the card gets turned off and then on and in my case makes my wireless connection.

Snow Leopard changes
In Snow Leopard the command remains, however now you need to run it as sudo. You also need to include the actual network device name AirPort is running on. You can get that by running the following command:

/usr/sbin/networksetup -listallhardwareports

You’ll see Airport listed and below it the device. If the machine does not have two Ethernet ports AirPort is commonly listed as “en1”

After you have that you include it in your command:

sudo /usr/sbin/networksetup -setairportpower en1 on

I’ve included these changes and a routine that will find the airport device in the Snow Leopard version of the script.

Click here to download a copy of the script for Tiger/Leopard:
Airport off and on

Click here to download a copy of the script for Snow Leopard:
Snow Leopard Airport off and on

networksetup is a great tool for administrators and even just people who want a little more control over their machines.

Enabling clear text passwords in Leopard with AppleScript

Note: The method for doing this in Snow Leopard is almost the same but has one slight change to it. Check out the post on doing enabling this in Snow Leopard for the changes.

Leopard, by default, has clear text passwords disabled for AFP connections. This is of course a very good thing to do. No one should be using clear text password connections anymore. However, there are still some older implementations of AFP out there on servers that require a clear text password. So, how do you enable them? By editing a property list or .plist file.

The file in question here is named “com.Apple.AppleShareClient.plist”. It’s located in the Library/Preferences folder in each users home folder. Now, there are a couple of ways to edit this file. If you have the Developer’s Tools installed you can use Property List editor to change that setting from “NO” to “YES”.

Or, if you prefer a command line approach you can use the defaults command to write your settings to the file.

defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES

If you’re not sure if clear text passwords are enabled you can use the “read” function in defaults to read the value

defaults read com.Apple.AppleShareClient afp_cleartext_allow

A returned value of “0” means it is disabled. A value of “1” means enabled.

If you have a lot of users that need to have this enabled or even checked that’s a lot of typing. So, once again AppleScript to the rescue.

This script will check the status of clear text passwords on launch. If it’s already enabled it will ask if the user wants to disable it. If it’s not enabled it will ask to enable it.

So, just launching the script will let you see if you need to do anything or not. Saving this an application and emailing it to users is a quick way to have them enable it if they need it and then disable it when the need is over without you having to walk over there and type everything a bunch of times.

If you would prefer to download a pre-complied script file click below:

Leopard Clear Text script

[codesyntax lang=”applescript” lines=”no”]
try
set clearStatus to (do shell script “defaults read com.Apple.AppleShareClient afp_cleartext_allow”) as number
on error
-the first command will throw an error if the afp_cleartext_allow setting does not exist
-if there is an error we’ll assume that the setting isn’t there and set our variable to the disabled setting
set clearStatus to 0
end try
-a status of “1” means it’s enabled. So ask if they want to disable it
if clearStatus is 1 then
display dialog “Do you want to disable clear text passwords?” buttons {“Cancel”, “Disable”} default button 2
if the button returned of the result is “Disable” then
do shell script “defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool NO”
set clearStatus to (do shell script “defaults read com.Apple.AppleShareClient afp_cleartext_allow”) as number
-check to make sure the change really took effect
if clearStatus is 0 then
display dialog “Clear text passwords have been disabled” buttons {“OK”}
else
display dialog “There was an error disabling clear text passwords!” buttons {“OK”}
end if
end if
else
display dialog “Do you want to enable clear text passwords?” buttons {“Cancel”, “Enable”} default button 2
if the button returned of the result is “Enable” then
do shell script “defaults write com.Apple.AppleShareClient afp_cleartext_allow -bool YES”
set clearStatus to (do shell script “defaults read com.Apple.AppleShareClient afp_cleartext_allow”) as number
-check to make sure the change really took effect
if clearStatus is 1 then
display dialog “Clear text passwords have been enabled” buttons {“OK”}
else
display dialog “There was an error enabling clear text passwords!” buttons {“OK”}
end if
end if
end if
[/codesyntax]

Making an alias with AppleScript

This is a simple one but can be very useful.

To make an alias (shortcut) that points to another file you just need the path to file itself and the place you want the alias.

For this example we’ll make an alias of the Apple Mail program on the desktop the current user.

[codesyntax lang=”applescript” lines=”no”]
tell application “Finder” to make new alias at (path to desktop folder) to file ((path to applications folder as text) & “Mail”)
[/codesyntax]

That’s all there is to it. Remember to take advantage of the built in “path to” calls in the Standard Additions dictionary. In Script Editor go to “Open Dictionary” and choose “Standard Additions” from the list. Type “path” in the search box to find it quickly.

Next »