@weixin
2015-07-06T13:58:06.000000Z
字数 8207
阅读 1458
osx
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Window" of menu "File" of menu bar 1
end tell
end tell
if I spell the application
to applicaiton
, here is the error :
expected end of line but found """.
This is not very explict.
tell application "Finder" to get the name of front Finder window
when you run this script, the finder have to be at front, otherwise you would get error.
tell application "Finder" to ¬
get the index of Finder window "Deskto
¬ is a symbol when u press opt + L, linebreaker in applescript.
tell application "Finder" to ¬
get the name of the front Finder window
result : "Desktop"
The value of the target property is a reference to the folder or disk whose contents are displayed in the Finder window. This value can be both read and changed. Let’s examine both actions.
tell application "Finder" to ¬
get the target of the front Finder window
result :
folder "Desktop" of folder "xwei" of folder "Users" of startup disk of application "Finder"
tell application "Finder" to ¬
set the target of the last Finder window to home
result :
folder "xwei" of folder "Users" of startup disk of application "Finder"
To construct an object reference to a specific Finder disk item, simply start with the item to be referenced, and list each folder or containing item of the object hierarchy until you’ve reached the disk containing the item.
for ex, what's the finder references it would be when I am at my home directory.
command + click
on the finder title to get the hierachy.
so the complelete references would be
folder xwei of folder Users of disk "Macintosh HD"
but the you also can use abbrevition, just home
is enough.
The toolbar visible property, introduced in Mac OS X 10.3 (Panther), has a value of true or false to indicate whether the toolbar is visible in the targeted Finder window. If the value is true, then the toolbar is visible. If the value is false, then the window is displayed without a toolbar.
if 'false'
if true
The next Finder window property we’ll examine is the current view property. You set the value of this property to display the contents of the Finder window in one of four enumerated views, icon view, list view, column view, and, in Leopard, flow view. Like the target and toolbar visible properties, you can both read and edit this property. Script 1.24 shows how to read it.
tell application "Finder" to ¬
get the current view of the last Finder window
result :
column view
tell application "Finder"
tell folder "Documents" of home
get the properties of the startup disk
end tell
end tell
you would get this :
because 'folder' cannot contain 'disk'.
change to this :
tell application "Finder"
tell folder "Documents" of home
end tell
get the properties of the startup disk
end tell
then it works:
{class:disk, name:"Macintosh HD", index:3, displayed name:"Macintosh HD", name extension:"", extension hidden:false, container:computer container of application "Finder", disk:startup disk of application "Finder", position:{-1, -1}, desktop position:{-1, -1}, bounds:{-33, -33, 31, 31}, kind:"Volume", label index:0, locked:false, description:missing value, comment:"", size:2.64870567936E+11, physical size:2.64870567936E+11, creation date:date "Tuesday, March 25, 2014 at 1:39:12 PM", modification date:date "Saturday, July 4, 2015 at 6:53:51 AM", icon:missing value, URL:"file:///", owner:"system", group:"root", owner privileges:read write, group privileges:read only, everyones privileges:read only, container window:container window of startup disk of application "Finder", id:-100, capacity:4.98948268032E+11, free space:2.34077700096E+11, ejectable:false, startup:true, format:Mac OS Extended format, journaling enabled:true, local volume:true, ignore privileges:false}
another example :
tell application "Finder"
get the first folder of the entire contents of folder "Documents" of home whose name is "astyle"
get the container of the result
end tell
what this code does? given a foldr name 'astyle' which are located at /home/Documents
, find its parent folder.
result :
older "Documents" of folder "xwei" of folder "Users" of startup disk of application "Finder"
tell application "Finder"
id of application file "Cisco AnyConnect Secure Mobility Client.app" of folder "Cisco" of folder "Applications" of the startup disk
end tell
result :
"com.cisco.Cisco-AnyConnect-Secure-Mobility-Client"
tell application "Finder"
id of application file "JavaToken.app" of folder "Defender" of folder "Quest Software" of folder "Applications" of the startup disk
end tell
result :
"????"
The make command is used to create scriptable objects
Scripters can use five types of item references in Mac OS X to refer to items on disk. We call them nested references, path references, alias references, POSIX paths, and file URLs.
convert the nested reference into a path by adding as string to the existing script
tell application "Finder" to get folder "Documents" of home as string
result :
"Macintosh HD:Users:xwei:Documents:"
QuickTime Player doesn’t know what a folder or document file is. The nested reference in the example uses terms from the Finder’s dictionary, such as document file and folder, which are not part of QuickTime Player’s scripting vocabulary.
Path references won’t work in most other applications either, because they are unique to the Finder application and a few others.
For references to files and folders to be usable in most applications, they must be in a generic format, recognized by all applications, called an alias reference.
object references:
folder "Macintosh HD:Users:sal:Documents:"
alias :
alias "Macintosh HD:Users:sal:Documents:"
Item references in UNIX are displayed as POSIX paths (POSIX stands for Portable Operating System for UNIX). An item reference in POSIX format is similar to the path part of alias references and path references, but the delimiter character is a forward slash (/) instead of a colon (:).
tell application "Finder" to get POSIX path of (folder "Documents" of home as alias)
result :
"/Users/xwei/Documents/"
unix references to object references:
"/Users/xwei/Documents/" as POSIX file
result :
file "Macintosh HD:Users:xwei:Documents:"
"/Users/xwei/Documents/" as POSIX file as alias
result :
alias "Macintosh HD:Users:xwei:Documents:"
tell application "Finder" to get the URL of folder "Documents" of home
result :
"file:///Users/xwei/Documents/"
tell application "Finder" to get item 1 of the startup disk
result :
folder "Applications" of startup disk of application "Finder"
by range :
tell application "Finder" to get items 2 thru 4 of the startup disk
The same list of items, referenced in the same order but using negative numbers, is as follows:
{item -4, item -3, item -2, item -1}
The ability to reference items in either direction allows you to locate the last item without knowing how many items are in the group. Just use index value −1.
tell application "Finder" to get items 2 thru -1 of the startup disk
or
tell application "Finder" to get every item of the startup disk
For example, instead of using item 5, you could write your index value as the fifth item, because AppleScript supports the descriptive indexes for the numeric values 1 through 10, as in first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, and tenth.
Or you can use descriptive suffixes for any integer value, as in the 1st item, the 23rd item, or the 4064th item.
Although using these optional index forms can make your scripts very easy to read and understand, there is one caveat regarding their use. When describing a range of items, you must use only integers, as in items 56 thru 112.
tell application "Finder" to get the front item of the startup disk
result :
folder "Applications" of startup disk of application "Finder"
or
tell application "Finder" to get the last item of the startup disk
result :
folder "Users" of startup disk of application "Finder"
or
tell application "Finder" to get the back item of the startup disk
result :
folder "Users" of the startup disk of application "Finder"
or
tell application "Finder" to get the middle item of the startup disk
front, back, last, middle, before, after, some, every
tell application "Finder" to get every item of the startup disk whose name begins with "S"
result :
{folder "System" of startup disk of application "Finder"}
Another way to request references to all matching items is to use the plural form of the class of the objects you are searching for, such as this:
items of the startup disk whose name begins with "S"
begin(s) with, start(s) with, end(s) with, contains, does not(doesn't) contain, is in, is contained by, is not(isn't) in
comes before
is less than
is not greater than or equal
is not greater than or equal to
isn't greater than or equal
isn't greater than or equal to
less than
>
comes after
greater than
is greater than
is not less than or equal
is not less than or equal to
isn't less than or equal
isn't less than or equal to
≠
<=
does not come after
doesn't come after
is less than or equal
is less than or equal to
is not greater than
isn't greater than
less than or equal
less than or equal to
≤
>=
does not come before
doesn't come before
greater than or equal
greater than or equal to
is greater than or equal
is greater than or equal to
≥
is not less than
isn't less than
equal
equals
equal to
is equal to+++
¤
does not equal
doesn't equal
is not
is not equal
is not equal to