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

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 »
11 total  XML / RSS feed