AppleScript_ The Definitive Guide - Matt Neuburg [200]
bundle resource location
Locates a file in the Resources folder of a bundle. Intended primarily for script bundles and applet bundles (and AppleScript Studio applications), so they can see inside themselves; doesn't work in Script Editor, but works fine in Script Debugger. You can optionally designate any bundle to look in (such as an application), and a subfolder within the bundle.
Examples
display dialog ((path to resource "description.rtfd") as string)
-- feathers:Users:mattneub:Library:Scripts:myScriptBundle.scptd:Contents:
Resources:description.rtfd
set f to path to resource "app.icns" in bundle ¬
alias "feathers:Applications:Mail.app:"
display dialog "Time to check your mail!" with icon f
Name
list disks
Synopsis
volume names
volume names
Gets the names of all mounted volumes. Returns a list of strings (meaning Unicode text).
Example
list disks -- {"feathers", "gromit", "Network"}
Name
list folder
Synopsis
folder contents
folder contents
Gets the names of all items within a folder. Includes invisible files and folders if you don't prevent it. Returns a list of strings (meaning Unicode text).
Example
list folder (path to home folder)
-- {".bash_history", ".CFUserTextEncoding", ".DS_Store", ".ssh", ...}
Name
info for
Synopsis
file/folder information
file/folder information
Gets information about an item on disk. Returns a file information record packed with useful stuff. If you ask for the info for a folder, the script may take some time to run, in order to sum the sizes of all the files within it; you can prevent this by saying without size.
Example
set uf to (path to home folder as string)
set L to list folder uf
set s to {}
repeat with f in L -- collect sizes of all items
set end of s to size of (info for file (uf & f))
end repeat
set maxItem to 0
set maxVal to 0
repeat with i from 1 to (count s) -- find biggest size
if item i of s > maxVal then
set maxItem to i
set maxVal to item i of s
end if
end repeat
display dialog "The biggest thing in your home folder is: " & item maxItem of L
Name
open for access
Synopsis
open file
open file
Opens a file for read access—optionally, for write access—creating the file as a text file if it doesn't exist (it does this even if you're opening for read access only; I regard this as a bug). Returns a file reference number that can be used with the other commands.
Example
set f to (path to desktop as string) & "newfile.txt"
set ff to open for access file f
close access ff
Name
read
Synopsis
read data
read data
Reads data from a file, optionally treating it as a specified datatype (for an example, see "Forming Unicode Text" in Chapter 13); the default is string (not Unicode text). There are options for where to start (character position values start at 1), how many characters to read, and where to stop. The using delimiter parameter is poorly documented: this is a list of one-character strings, any of which will be used to break the data into a single-level list of strings (which will lack all the delimiter characters). The until and before parameters fail when reading as string if they are out of the basic ASCII range (over 128), but they work using Unicode text.
Example
set f to (path to desktop as string) & "someUTF16file.txt"
set ff to open for access file f
set d1 to read ff as Unicode text
d1 -- "Mannie¬Moe¬Jack"
-- testingdelimiter parameter
set notsign to "¬" as Unicode text
set L to read ff from 1 as Unicode text using delimiter notsign
L -- {"Mannie", "Moe", "Jack"}
-- testingbefore parameter
set d2 to read ff from 1 as Unicode text before notsign
close access ff
d2 -- "Mannie"
Name
write
Synopsis
write data
write data
Writes data to a file, optionally treating it as a specified datatype; thus you can store any kind of data in a text file and retrieve it later (it will be read correctly if you specify the same class when writing and when reading). There are options for where to start and how much data