[关闭]
@weixin 2015-07-06T13:58:06.000000Z 字数 8207 阅读 1458

applescript example

osx


  1. tell application "Safari" to activate
  2. tell application "System Events"
  3. tell process "Safari"
  4. click menu item "New Window" of menu "File" of menu bar 1
  5. end tell
  6. end tell

if I spell the application to applicaiton, here is the error :
expected end of line but found """. This is not very explict.

  1. 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.

  1. tell application "Finder" to ¬
  2. get the index of Finder window "Deskto

¬ is a symbol when u press opt + L, linebreaker in applescript.

  1. tell application "Finder" to ¬
  2. 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.

  1. tell application "Finder" to ¬
  2. get the target of the front Finder window

result :

  1. folder "Desktop" of folder "xwei" of folder "Users" of startup disk of application "Finder"
  1. tell application "Finder" to ¬
  2. set the target of the last Finder window to home

result :

  1. folder "xwei" of folder "Users" of startup disk of application "Finder"

Finder references

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.

toolbar visibility

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
此处输入图片的描述

status bar visibility

the current view property

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.

  1. tell application "Finder" to ¬
  2. get the current view of the last Finder window

result :

  1. column view

hierachy

  1. tell application "Finder"
  2. tell folder "Documents" of home
  3. get the properties of the startup disk
  4. end tell
  5. end tell

you would get this :
此处输入图片的描述

because 'folder' cannot contain 'disk'.

change to this :

  1. tell application "Finder"
  2. tell folder "Documents" of home
  3. end tell
  4. get the properties of the startup disk
  5. end tell

then it works:

  1. {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 :

  1. tell application "Finder"
  2. get the first folder of the entire contents of folder "Documents" of home whose name is "astyle"
  3. get the container of the result
  4. end tell

what this code does? given a foldr name 'astyle' which are located at /home/Documents, find its parent folder.

result :

  1. older "Documents" of folder "xwei" of folder "Users" of startup disk of application "Finder"
  1. tell application "Finder"
  2. id of application file "Cisco AnyConnect Secure Mobility Client.app" of folder "Cisco" of folder "Applications" of the startup disk
  3. end tell

result :

  1. "com.cisco.Cisco-AnyConnect-Secure-Mobility-Client"
  1. tell application "Finder"
  2. id of application file "JavaToken.app" of folder "Defender" of folder "Quest Software" of folder "Applications" of the startup disk
  3. end tell

result :

  1. "????"

creating objects

The make command is used to create scriptable objects

identifying 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.

object references

path references

convert the nested reference into a path by adding as string to the existing script

  1. tell application "Finder" to get folder "Documents" of home as string

result :

  1. "Macintosh HD:Users:xwei:Documents:"

alias references

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:

  1. folder "Macintosh HD:Users:sal:Documents:"

alias :

  1. alias "Macintosh HD:Users:sal:Documents:"

unix references

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 (:).

  1. tell application "Finder" to get POSIX path of (folder "Documents" of home as alias)

result :

  1. "/Users/xwei/Documents/"

unix references to object references:

  1. "/Users/xwei/Documents/" as POSIX file

result :

  1. file "Macintosh HD:Users:xwei:Documents:"
  1. "/Users/xwei/Documents/" as POSIX file as alias

result :

  1. alias "Macintosh HD:Users:xwei:Documents:"

file urls

  1. tell application "Finder" to get the URL of folder "Documents" of home

result :

  1. "file:///Users/xwei/Documents/"

find items

find items by index

  1. tell application "Finder" to get item 1 of the startup disk

result :

  1. folder "Applications" of startup disk of application "Finder"

by range :

  1. 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:

  1. {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.

  1. tell application "Finder" to get items 2 thru -1 of the startup disk

or

  1. tell application "Finder" to get every item of the startup disk

find items by descriptive index

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.

  1. tell application "Finder" to get the front item of the startup disk

result :

  1. folder "Applications" of startup disk of application "Finder"

or

  1. tell application "Finder" to get the last item of the startup disk

result :

  1. folder "Users" of startup disk of application "Finder"

or

  1. tell application "Finder" to get the back item of the startup disk

result :

  1. folder "Users" of the startup disk of application "Finder"

or

  1. tell application "Finder" to get the middle item of the startup disk

front, back, last, middle, before, after, some, every

find items by property

  1. tell application "Finder" to get every item of the startup disk whose name begins with "S"

result :

  1. {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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注