AppleScript Snippets   TOC  Email  Viewing Notes

Contents

About this page

Made with CSS

Due to the complexity of AppleScript syntax, I have changed from using HTML <FONT> tags to using real CSS1 styles. As a result Navigator 3.0 will no longer show the styles. Thus, this page is best viewed using Explorer 3.0. (You could try Nav4, but that usurps over 16MB of RAM while IE3 [for the Mac] takes only 4MB.)

General Snippets:

SetDateTime

See my Directory of Files page to download this script.

Finder Snippets:

SignatureToApp

-- this returns "Tex-Edit Plus"
tell application "Finder"
    set apname to name of application file id "TBB6"
end tell

tell application "Finder"
    open selection using application file id "TBB6"
end tell

Processes

tell application "Finder"
    name of every process whose file type is not "FNDR" and visible is true
    name of first process whose visible is true
    name of 2nd process whose visible is true
    -- the following three are equivalent:
    name of front process
    name of process -1 whose visible is true
    name of last process whose visible is true
end tell

on GetFrontmostApp()
    repeat
        tell application "Finder"
            try
                set apname to name of first process whose frontmost is true
            on error --because Finder 7.5.1 doesn't list itself...
                if frontmost then
                    set apname to "Finder"
                else --just to be on the safe side!
                    set apname to "
                end if
            end try
            if apname ­ " then exit repeat
        end tell
    end repeat
    return apname
end GetFrontmostApp

Double-Tell

The "double-tell" technique is a way to talk to an application without first knowing its name. The Finder looks for the application by its creator code (aka signature) instead. MacOS 8.0 introduced a couple of bugs which break double-tells previously written for MacOS 7.5 and 7.6; however, it is possible to write scripts which work for all scriptable Finders:

tell application "Finder"
    open application file id "CSOm"
    -- the psn coercion is a workaround for two Finder 8.0 bugs
    set app_ref to (first process whose creator type is "CSOm") as Çclass psn È
end tell

Now that we have a reference to the application, we can talk to it in one of two ways. The first way is the double-tell, which is useful in pre-compiled scripts where the commands will be sent to a specific application. The second way is useful where the script will not be compiled until run time, or where you are compiling for multiple application which support the same events but have different terminologies for the events (such as Netscape Navigator and MS Internet Explorer).

tell application "Eudora Pro 3.1" to tell app_ref
    subject of message -1 of mailbox "In"
end tell

tell app_ref
    Çclass euSuÈ of Çclass euMSÈ -1 of Çclass euMBÈ "In"
end tell

Updating

tell application "Finder"
    update folder "BOO:PROGRAMS:COM:_ Downloads:"
end tell

Aliases

tell application "Finder"
    make new alias file to fileref at folder myfolder with properties {name:aliasname}
end tell

Clippings

For info on these special files, see my Eudora mailbox archive from the MacScrpt mailing list (messages from March - May 1995).  (Sorry this link was broken for a long time and I never noticed...)

Extra Items Deleter

property keepfolder : "
property keepfiles : "

tell application "Finder"
    set flist to name of every file in folder keepfolder
    repeat with curfile in flist
        if curfile is not in keepfiles then
            delete file ((myfolder as text) & curfile)
        end if
    end repeat
end tell

on open (theFolder)
    tell application "Finder"
        set keepfolder to theFolder as alias
        set keepfiles to name of every file in folder keepfolder
    end tell
end open

Coercions:

typeApplicationSignature

While Netscape's register protocol command works fine with a 4-character creator code as text, unregister protocol insists on a class of typeApplicationSignature. In order to build this from a variable, you need the Programmer's Tool osax (it's archived as pgmTool) to cast the data type:

set crea to "FTCh"
cast crea to crea
set apsig to class of result

tell application "Netscape Navigatorª 2.0"
    register protocol apsig for protocol "ftp"
end tell

tell application "Netscape Navigatorª 2.0"
    unregister protocol apsig
end tell

(* Quit and relaunch Netscape to be sure the new settings "take" *)

Styled Text

The following pair of routines splits styled text into 'TEXT'/'styl' parts, and mixes them back into styled text again. You can use these for reading/writing text clippings or for creating your own SimpleText application. Ed Lai's Programmer's Tool osax is required.

on STXT_to_TEXTstyl(theSTXT)
    cast theSTXT to "styl"
    set {theTEXT, stylData} toclass ktxtÈ of result, Çclass kstyÈ of result}
    cast stylData to "TEXT"
    copy result to thestyl
    return {theTEXT, thestyl}
end STXT_to_TEXTstyl

on TEXTstyl_to_STXT(textData, stylData)
    cast stylData to "styl"
    copy result to k_styl
    set s to {class:scrap styles, Çclass ktxtÈ:textData, Çclass kstyÈ:k_styl}
    cast s to "STXT"
    return result
end TEXTstyl_to_STXT

Classes

Have you ever wanted/needed to get the 4-character type of a class? I needed to do this for my "Clip-n-Stash" program so that I could display the types the way ScrapIt Pro does, so I wrote this handler:

on ClassToType(theClass)
    cast theClass to "TEXT" -- Programmer's Tool by Ed Lai
    return result
end ClassToType

***For a list of classes, constants, and their codes, see my Classes & Constants page.

Gestalt Conversions

When Gestalt returns a version number, it is in the form of a long integer corresponding to a hexadecimal representation of the version number. For example, QuickDraw 2.3.9 is encoded as $"00000239", which becomes 569 decimal. When you get the number (such as 569) from gestalt, you need to convert it back into hex in order to compare it with your reference value. Many gestalt selectors pack two version numbers together, such as the AppleScript version which is the rightmost half of the hexadecimal value. Some other gestalt selectors are sets of 32 flags, so I've written Num32ToHex() and Num32ToFlag() functions, both shown below.

tell application "Finder"
    computer "drag"
end tell
return Num32ToFlag(result, 0) -- Drag Manager present?

tell application "Finder"
    computer "fndr"
end tell
return Num32ToFlag(result, 6) -- Finder clippings supported?

tell application "Finder"
    computer "help"
end tell
return Num32ToFlag(result, 31) -- AppleGuide present?

tell application "Finder"
    computer "qd " -- (two spaces)
end tell
return Num32ToHex(result)

tell application "Finder"
    computer "ascv"
end tell
Num32ToHex(result)
set asver to (text 5 thru 8 of result)
if (asver < "0110") then display dialog "You need AppleScript 1.1 or later."

on Num32ToHex(num)
    if num < 0 then --convert signed integer to unsigned real
        set num to (2 ^ 32) + num
    end if
    set out to "
    repeat with idx from 7 to 0 by -1
        set bas to (16 ^ idx) as integer
        if num ³ bas then
            set mult to ((num - (num mod bas)) / bas) as integer
            set out to out & character mult of "123456789ABCDEF"
            set num to (num - bas * mult)
        else
            set out to out & "0"
        end if
    end repeat
    return out
end Num32ToHex

on Num32ToFlag(num, bitnum)
    if num < 0 then --convert signed integer to unsigned real
        set num to (2 ^ 32) + num
    end if
    if bitnum < 0 or bitnum > 31 then
        error "Num32ToFlag(num,¥bitnum)" & Â
            return & "Value must be in range 0-31."
    end if
    repeat with exp from 31 to 0 by -1
        set curval to (2 ^ exp)
        if num ³ curval then
            set flag to true
            set num to (num - curval)
        else
            set flag to false
        end if
        if bitnum = exp then exit repeat
    end repeat
    return flag
end Num32ToFlag


Green -- application keywords (Espy Sans 10, bold)
Blue -- variables (Avant Garde 10)
Orange -- operators (Geneva 10)
Burgundy -- language keywords (Geneva 10, bold)
Brown -- literals (Monaco 10)


Navigate to:


(wik, September 7, 1997) 63092 visits since 96.04.25