Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
15 total  XML / RSS feed 

Reliably change Adium Status

This applescript snippet will change the away/available status of your Adium accounts. This is the only applescript I have found that has worked.


-- set to available
do_menu("Adium", "Status", "Available")

-- set to away
do_menu("Adium", "Status", "Away")


on do_menu(app_name, menu_name, menu_item)
        try
                -- bring the target application to the front
                tell application app_name
                        activate
                end tell
                tell application "System Events"
                        tell process app_name
                                tell menu bar 1
                                        tell menu bar item menu_name
                                                tell menu menu_name
                                                        click menu item menu_item
                                                end tell
                                        end tell
                                end tell
                        end tell
                end tell
                return true
        on error error_message
                return false
        end try
end do_menu

AppleScript to test if application is running

By name:
tell application "System Events" to (name of processes) contains "iTunes"


By creator type, in case it might have a version # appended:
tell application "System Events" to (creator type of processes) contains "InDn"


Grab creator type dynamically like so:
tell application "Finder" to creator type of (selection as alias)


From http://applescriptsourcebook.com/viewtopic.php?pid=46830

quicksilver tags in geektool script version 3

// can anyone tell me what this is? Is it applescript?
How do I call it from geektool? I found this here
http://theappleblog.com/community/development/588-displaying-quicksiler-tags/#post6596

set QuicksilverTags to (path to home folder as string) & "Library:Caches:Quicksilver:Indexes:QSPresetQSFileTagsPlugIn.qsindex"

do shell script "grep string " & (quoted form of POSIX path of QuicksilverTags) & " | awk {'print $1'}"

set theTags to result

--> From the MacScripter.net Text Delimiters Tutorial.
set newText to switchText of theTags from "qs.tag.file" to ""
to switchText of currentText from SearchString to ReplaceString
        set storedDelimiters to AppleScript's text item delimiters
        set AppleScript's text item delimiters to SearchString
        set currentText to currentText's text items
        set AppleScript's text item delimiters to ReplaceString
        set currentText to currentText as Unicode text
        set AppleScript's text item delimiters to storedDelimiters
        
        currentText
        
        set this_text to currentText
        
        set new_text to ""
        
        --> From the MacScripter Forum.
        repeat with myPara in paragraphs of this_text
                if new_text does not contain myPara then set new_text to new_text & myPara & return
        end repeat
        set new_text to (characters 1 thru -2 of new_text) as text
        
        set more_text to new_text
        set replace_text to ""
        
        --> From MacScripter Forum.
        repeat with myPara in paragraphs of more_text
                
                if replace_text does not contain myPara then set replace_text to replace_text & (text 9 thru -10 of myPara) & return
        end repeat
        
        set output to "Tags: " & return & replace_text
        
end switchText -- the end of the handler.

quicksilver tags in geektool

// this code taken from http://bbs.applescript.net/viewtopic.php?id=20330

do shell script "grep string /Users/username/Desktop/QSPresetQSFileTagsPlugIn.qsindex | awk {'print $1'}"

set theTags to result

set newText to switchText of theTags from "qs.tag.file" to ""

to switchText of currentText from SearchString to ReplaceString -- the handler
   
   set storedDelimiters to AppleScript's text item delimiters
   -- this simply stores the current value of AppleScript's AppleScript's text item delimiters
   -- so they can be restored later (thus helping to avoid potential problems elsewhere).
   -- Remember, we always set them back to what they were.
   
   set AppleScript's text item delimiters to SearchString
   -- AppleScript's AppleScript's text item delimiters are now set to "Purple"
   
   set currentText to currentText's text items -- note we have changed currentText's value
   -- create a list of text items from the original text, separated at the points where the
   -- current text item delimiter ("Purple") appeared.
   --> {"What, ", " Shoes?"} - Note that the spaces and punctuation are retained.
   
   set AppleScript's text item delimiters to ReplaceString
   -- AppleScript's AppleScript's text item delimiters are now set to "Green"
   
   set currentText to currentText as Unicode text
   -- coerce the list {"What, ", " Shoes?"} to Unicode text. This operation will also
   -- insert the current value of AppleScript's AppleScript's text item delimiters ("Green")
   -- between each of the listed items
   
   --> "What, Green Shoes?"
   
   set AppleScript's text item delimiters to storedDelimiters
   -- restore the value of AppleScript's AppleScript's text item delimiters
   -- to whatever they were on entering the subroutine. Remember that a call to this
   -- might have been made from within a section of script that had the TIDs set to
   -- something else. Hand the result back with the TIDs as they were.
   
   currentText
   -- return the now modified text (and restored TIDs) -- "What, Green Shoes?"
   
   
   set this_text to currentText
   
   set new_text to ""
   
   --Loop through paragraphs of old text
   repeat with myPara in paragraphs of this_text
       --Check for paragraph’s contents in new text
       --If not there add new text to end of new text
       if new_text does not contain myPara then set new_text to new_text & myPara & return
   end repeat
   --Remove final return
   set new_text to (characters 1 thru -2 of new_text) as text
   
end switchText -- the end of the handler.

XML or HTML Tag Parser

This handler is intended to accept a string input, and remove the contents of a specific tag within that sting input. This is great for getting string contents out of xml or html files. For example, if you want to get the title of an html document you call
tagParse(yourFileContents, "", ")
it would return the text between the two title tags.
on tagParse(thisText, openTag, closeTag)
        set new_text to ""
        set strlen to the length of thisText
        set taglen to (the length of openTag) - 1
        set closeTagLen to (the length of closeTag) - 1
        set stridx to 1
        repeat until (stridx + taglen) is greater than strlen
                set thisString to the contents of characters stridx through (stridx + taglen) of thisText as text
                log thisString
                if thisString is openTag then
                        set firstChar to stridx + taglen + 1
                        --display dialog "the title text starts at character " & firstChar
                        repeat until (stridx + closeTagLen) is greater than strlen
                                set thisString to the contents of characters stridx through (stridx + closeTagLen) of thisText as text
                                log thisString
                                if thisString is closeTag then
                                        return the contents of characters firstChar through (stridx - 1) of thisText as text
                                end if
                                set stridx to stridx + 1
                        end repeat
                end if
                set stridx to stridx + 1
        end repeat
        return "Error:: tag " & openTag & " not found."
end tagParse

Log items to tab delimited text file

this script is intended to be used to write logs to a tab delimited text file

logfilename is a string
theFields must be a list
logfilename is just the name of your log file

on logFile(theFields, logFileName)
        try
                tell application "Finder"
                        if not (file ((path to desktop folder as text) & logFileName) exists) then
                                set theLogFile to make file at folder (path to desktop folder as text)
                                set the name of theLogFile to logFileName
                                set myFile to open for access file ((path to desktop folder as text) & logFileName) with write permission
                        else
                                set myFile to open for access file ((path to desktop folder as text) & logFileName) with write permission
                        end if
                        set endOfFile to get eof myFile
                        repeat with eachField in theFields
                                set theFieldString to theFieldString & item eachField of theFields & tab
                        end repeat
                        write (theFieldString & return) to myFile starting at (endOfFile + 1)
                        close access myFile
                end tell
        on error err
                display dialog "log file error" & err giving up after 5
                try
                        close access ((path to desktop folder as text) & logFileName)
                end try
        end try
end logFile

Folder Maker

This handler creates a folder of name NameOnly to the location of thePath.
thePath is the path to the folder you want the folder to be created in
NameOnly is the name you want to give the folder

both handler inputs should be strings (e.g. Drive:folder:)
on folderMaker(thePath, NameOnly)
        tell application "Finder"
                if (folder (thePath & NameOnly) exists) is not true then
                        try
                                make new folder at folder (thePath) with properties {name:NameOnly}
                        on error the_error
                                display dialog the_error giving up after 10
                        end try
                end if
        end tell
end folderMaker

Remove Files older than X days

This script is intended to be in your script menu, or run as an applet.
It will ask you to choose a path to a folder that you want to clear out
files older then X days. It runs very fast because it uses the do shell
script command rather than the finder to get the time stamp of the
file. It's safe to use on network drives as well.
try
        set myFolder to (choose folder)
        set myTime to display dialog "Delete files older than how many days?" default answer "60"
        set myWildcard to display dialog "Enter a wildcard search string." default answer "PV*.txt"
        set myScript to "find \"" & POSIX path of myFolder & "\" -type f -name \"" & text returned of myWildcard & "\" -mtime +" & text returned of myTime & " -exec rm {} \\;"
        do shell script myScript
        display dialog "Deletion completed successfully"
on error err
        display dialog err
end try

Load Text File to string

The purpose of this handler is to load a text file from a posix path to a string

on loadTextFile(thepath)
        try
                set thestring to ""
                set theScript to ("cat " & "'" & thepath & "'")
                set thestring to do shell script theScript
                return thestring
        on error err
                log ("Cat text file error: " & err)
        end try
end loadTextFile

Is a file busy

Uses a do shell script to search for busy files

on isFileBusy(thePath)
--Tests to see if a file is in use
        try
                set myscript to "if ( lsof -Fp " & thePath & " | grep -q p[0-9]* ) then echo 'file is busy'; else echo 'not busy';fi"
                set myResult to do shell script myscript
                if myResult is "file is busy" then
                        return true
                else
                        return false
                end if
        on error err
                Display Dialog ("Error: isFileBusy " & err) giving up after 5
        end try
end isFileBusy

Mount Multiple Volumes or Disks

This snippet will mount required drives for your scripts. set the requiredDrives

set requiredDrives to {{theName:"drive1name", theMountScript:"afp://username:password@ip/drive1name"}, {theName:"drive2name", theMountScript:"afp://username:password@ip/drive2name"}} as list

set theDisks to list disks

repeat with i from 1 to (length of requiredDrives)
        if theDisks does not contain theName of item i of requiredDrives then
                mount volume theMountScript of item i of requiredDrives
        end if
end repeat

AppleScript String Parsing

This example will find the word "work" in the string "Bob went to work." and replace it with "the beach".

set myResult to snr("Bob went to work.", "work", "the beach")
display dialog myResult

(**** fast search and replace methods ****)
on snr(the_string, search_string, replace_string)
        return my list_to_string((my string_to_list(the_string, search_string)), replace_string)
end snr

on list_to_string(the_list, the_delim)
        my atid(the_delim)
        set the_string to (every text item of the_list) as string
        my atid("")
        return the_string
end list_to_string

on string_to_list(the_string, the_delim)
        my atid(the_delim)
        set the_list to (every text item of the_string) as list
        my atid("")
        return the_list
end string_to_list

on atid(the_delim)
        set AppleScript's text item delimiters to the_delim
end atid

Sort Apple Clipboard Contents

This applescript will convert anything in the finder clipboard to text, and then sort that text. Procedure is to copy an item. Run this script from the script menu, then paste.

set the clipboard to list_to_string(ASCII_Sort(string_to_listclass ktxt» of ((the clipboard as text) as record), return)), return)

on ASCII_Sort(my_list)
        --from apple
        set the index_list to {}
        set the sorted_list to {}
        repeat (the number of items in my_list) times
                set the low_item to ""
                repeat with i from 1 to (number of items in my_list)
                        if i is not in the index_list then
                                set this_item to item i of my_list as text
                                if the low_item is "" then
                                        set the low_item to this_item
                                        set the low_item_index to i
                                else if this_item comes before the low_item then
                                        set the low_item to this_item
                                        set the low_item_index to i
                                end if
                        end if
                end repeat
                set the end of sorted_list to the low_item
                set the end of the index_list to the low_item_index
        end repeat
        return the sorted_list
end ASCII_Sort


on snr(the_string, search_string, replace_string)
        return my list_to_string((my list_to_string(the_string, search_string)), replace_string)
end snr

on list_to_string(the_list, the_delim)
        my atid(the_delim)
        set the_string to (every text item of the_list) as string
        my atid("")
        return the_string
end list_to_string

on string_to_list(the_string, the_delim)
        my atid(the_delim)
        set the_list to (every text item of the_string) as list
        my atid("")
        return the_list
end string_to_list

on atid(the_delim)
        set AppleScript's text item delimiters to the_delim
end atid

AppleScript code to get the value of the home folder using "path to" and "home folder"

This is a AppleScript code snippet to get the path value of the home folder (e.g, "iBook Hard Drive:Users:stevejobs:").

set myPath to (path to home folder) as string
display dialog "myPath is " & myPath

Mute Remote Macintosh

Sometimes I am in bed and too lazy to get out to mute my G5. I can just grab my iBook, SSH into the G5 and run the following command:

osascript -e 'set volume output muted true'


brilliant!
« Newer Snippets
Older Snippets »
15 total  XML / RSS feed