Posts RSS Comments RSS 48 Posts and 155 Comments till now

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]

Using Terminal to control Spotlight

Spotlight is the Macintosh technology that lets you search for things on your Mac. When you search for something like “dog” it not only finds that letter combination in the names of files but searchs the contents of the files for that term as well.

Occasionally Spotlight goes a little nuts and does things like continuously indexing your entire machine, which can slow things down. A good fix for this problem is to simply delete the Spotlight index and let it recreate itself. This can also fix problems where things aren’t showing up in a search that you know are there. The tool we use to do this is a command line tool called mdutil

To delete a Spotlight index enter the following in Terminal. Spotlight will automatically begin to rebuild the index.

sudo mdutil -E volume_name

If you only have one hard drive or partition on your machine or you want to rebuild the index on the drive you’re booted from use this syntax:

sudo mdutil -E /

If you want to rebuild the index on a different drive then your boot drive use this syntax. We’ll assume the drive is name “Extra Stuff”:

sudo mdutil-E "/Volumes/Extra Stuff"

If, for some reason, you find you need to completely turn off Spotlight use this command.

sudo mdutil -i on/off volume_name

To turn off Spotlight indexing on your boot volume use this syntax:

sudo mdutil -i off /

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.

Check your current IP address with AppleScript

This script will get your current “real world” IP address. It’s very useful when you’re using a router giving out internal addresses and you need to know the address the world is seeing for you. It will also store that IP address so you can have this script run on a schedule and only report if your IP address has changed.

Click here to download the script

[codesyntax lang=”applescript” lines=”no”]
property theIP : “”
set TheResult to (do shell script “curl -f http://checkip.dyndns.org”)
set Olddelim to AppleScript’s text item delimiters
try
set AppleScript’s text item delimiters to “Current IP Address: ”
set LongIP to item 2 of every text item of TheResult as text
set AppleScript’s text item delimiters to “”
set stopPoint to (offset of “< " in LongIP) set myip to characters 1 through (stopPoint - 1) of LongIP as text if theIP is "" then set theIP to myip display dialog "Your IP is : " & myip end if if myip is not equal to theIP then display dialog "Your IP is : " & myip set theIP to myip end if set AppleScript's text item delimiters to Olddelim on error set AppleScript's text item delimiters to Olddelim end try [/codesyntax]

Delete printers using AppleScript

I recently had a request for a script that would delete printers from a lab build. The user wanted to make one system build for all of his labs with all of the printers installed. Then, just run a script to select the correct printer for that lab and delete the others. Here’s the script I came up with. With this script you can choose more then one printer if that is more appropriate.


--get the name of every printer
tell application "Printer Setup Utility"
set theList to the name of every printer as list
end tell

--present the list to choose the printer from
choose from list theList with title "Lab Printer" with prompt "Pick the correct printer for this lab" with multiple selections allowed
set theItem to the result as list

--loop through the list and delete any printer not in the list theItem
repeat with x from 1 to the count of theList
if theItem does not contain (item x of theList) then
do shell script "lpadmin -x " & (item x of theList)
end if
end repeat

Pinging a server using AppleScript

I’ve recently had a machine that suddenly seems to lose it’s network connection. I can’t figure out why but it always comes back after a reboot. The big problem with this is it’s my backup server and having that off the network is not good. So, I came up with an AppleScript that runs each hour to see if we’re still talking to the world. If not then I reboot the machine.

Here’s the basic script:


try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
delay 60
try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
do shell script "/sbin/shutdown -r now"
end try
end try

Breaking down the script

set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"

This executes the command line version of ping to see if we’re on the network. The “-o” means quit pinging as soon as we get a reply. Don’t want to flood Google with pings! The “-c 5” says only send 5 pings at the most.

If there is no response for what ever reason it throws an error. So the “on error” part of the script catches that and tells the script to delay itself for 60 seconds and then continue.

After 60 seconds we try again. If we get an error again the we tell the machine to restart itself using “shutdown -r”.

I run this script as a System cron job, running once an hour. By running as system I don’t have to include any passwords and if it needs to shutdown most likely no active processes can get in the way. I use Cronnix to set up my cron jobs. Saving the job as “ping.scpt” the cron job would look like this:

/usr/bin/osascript /path/to/ping.scpt

Using AppleScript to set the default printer

Here’s a simple little script that you can use to reset which printer is listed as default. You can set it up as a Login Item and have it run every time you log in to reset the default printer.

Get the name of the printer you want from Printer Setup Utility. You want the actual name displayed in the Printer Setup Window.

In AppleScript the term “current printer” refers to the currently selected printer, which is always the current default printer.

After you have the name place in this script, replacing the words “Epson Inkjet Printer”.

Download the compiled script here:
Default Printer Script

[codesyntax lang=”applescript” lines=”no”]
tell application “Printer Setup Utility”
set the_printer to the current printer
set the_name to the name of the_printer
if the_name is not “Epson Inkjet Printer” then
set the_count to the count of printers
repeat with x from 1 to the_count
if the name of printer x is “Epson Inkjet Printer” then
set the current printer to printer x
end if
end repeat
end if
quit
end tell
[/codesyntax]

The script gets the name of the current default printer. Then it loops through all the printers in the list looking for the name of the one you want. When the script comes to that name it sets it to be the current printer. Calling Printer Setup Utility in a script always launches it so I put a “quit” statement at the end so we don’t have Printer Setup Utility running afterwards.

Using Applescript to kill off a process

I use AppleScript to clean up some temp spaces on my lab machines. It’s easy to use and gives me most of what I need without a lot of shell programming. However, to do some of the cleaning I have to call up the Finder. That’s fine if the machine is already logged in but what if it isn’t? My cleanup script runs around 2 AM each morning when no ones around and no one’s logged in.

The issue with that is since the script runs as root it launchs the Finder as root. And leaves it running as root. So if you were to go to one of those machines in the morning you’d see the Finder running underneath the Login window. If you log in as a normal user it doesn’t restart the Finder, just leaves it running as Root. So, anybody could come along, log in and have complete root privileges to the entire system until they log out. Can you say security issue?

So, I have a small script that I keep around for just such an occasion. It does a search and destroy for the Finder process ID and kills it at the end of the script so we aren’t leaving root hanging around.

[codesyntax lang=”applescript” lines=”no”]
set app_name to “Finder”
set the_pid to (do shell script “ps ax | grep ” & (quoted form of app_name) & ” | grep -v grep | awk ‘{print $1}'”)
if the_pid is not “” then do shell script (“kill -9 ” & the_pid)
[/codesyntax]

First we set the variable “app_name” to the name of the process we want to kill.

Next we set the variable “the_pid” to the process ID of that process. It takes a little fancy shell scripting and text manipulation to get that.

Finally, if we did find it then we send the “kill -9” command with the process ID to kill it off. Using “kill -9” is basically saying “Die and no back talk”.

Now you’ve successfully killed off the Finder running as root and closed the hole. You can use this script to kill off other processes as well.

Adding and deleting login items using AppleScript

It’s a fairly simple process to script adding and deleting login items using applescript. You can save these scripts as applications and send them out to users. Then they can run it themselves and add or remove a login item. You can have applications open on login or volumes mount on login.

Adding a login item

For adding a login item we’ll use the example of mounting a remote volume. I usually find that it works best to actually mount the item first and then add the login item. For this example we’ll assume we’re connecting to an SMB share named “home” on a Windows server.

[codesyntax lang=”applescript” lines=”no”]
mount volume “smb://server.example.com/home”
tell application “Finder”
--check to see if the volume mounted
if disk “home” exists then
display dialog “Success”
--this “tell” statement makes the actual login item
tell application “System Events”
make login item at end with properties {path:”/Volumes/home”, kind:volume}
end tell
else
--if it doesn’t work let the user know
display dialog “Oops! This problem happened:” & return & errmsg
end if
end tell
[/codesyntax]

So, in the example above we mount the server share first. Then, we check to see if it’s actually there. If so we tell the user we’ve succeeded with a dialog box and make the login item. If not we let them know there was a problem and display the error message so they can tell tech support.

Notice the “kind” section of that line. You need to tell it what you’re adding (a volume, an application, etc.).

Deleting a login item

Deleting a login item is even easier.

[codesyntax lang=”applescript” lines=”no”]
tell application “System Events”
--Find out what login items we have
get the name of every login item
--see if the item we want exists. If so then delete it
if login item “home” exists then
delete login item “home”
else
display dialog “That login item doesn’t exist”
end if
end tell
[/codesyntax]

We check to see what login items we have. If the one we want to delete exists we go ahead and nuke it.

« Previous PageNext Page »