Macs in Chemistry

Insanely great science

 

An Applescript to Extract a Safari Extension Archive

With With Safari 5 one of the most interesting new features is the support for Extensions. If you’ve ever used Firefox or Chrome you might be familiar with these. For more details you might want to take a look at Mac OS X Tips, where these is a detailed description and links to useful Extensions. There is also the Apple Developer page that gives more details of how to create your own extension

One of the best ways to learn how to create extensions is to examine other peoples creations, you can't do this directly because they are stored as compressed archives, Mac OS X Tips has a step by ste guide to extracting these archives using the Terminal. However if you are not comfortable using the Terminal then the Applescript below will do this for you. The first line gets the path to the desktop, this is where the extracted archive will be created (you should be able to edit this if you perfer to have them created elsewhere). The next part asks for the location of the file to be extracted, creates POSIX paths. Creation of the Shell command is slightly unusual in that we want to issue two commands. Each invocation of do shell script uses a new shell process, so state such as changes to variables and the working directory is not saved from one to the next. To do several commands in a single invocation, separate the commands with semicolons. Simply add to your Script Menu for easy access. The Script Menu can be added as a Menu Extra to allow easy access to many AppleScript scripts from the menu bar (Mac OS X ships with several scripts that are ready to use to accomplish some common tasks). You can also add your own scripts to this menu. To enable the Script Menu, run AppleScript Utility (located at /Applications/AppleScript/AppleScript Utility) and check the "Show Script Menu in menu bar" checkbox.

--Script to view code of Safari extension

set user_path to (path to desktop) as text

set posix_result_file to quoted form of POSIX path of user_path

set this_file to choose file
set this_file_text to (this_file as text)
tell application "Finder" to set file_name to (name of this_file)

display dialog "Extract " & file_name

--get the posix path to chosen file
set posix_this_file to quoted form of POSIX path of this_file

--Each invocation of do shell script uses a new shell process, so state such as changes to variables and the working directory is not saved from one to the next. To do several commands in a single invocation, separate the commands with semicolons like this

set shell_script to "cd " & posix_result_file & "; xar -xf " & posix_this_file


do shell script shell_script

You can download the script from here