Applescript Do Shell Script Variable Assignment

Hello.

I was thinking more of a pure applescript that gets the url from the window of the frontmost application while it is run from the script menu. If you want shortcut keys, then you can download the free version of FastScripts that gives you 20 some short-cuts to scripts. (That is enough for me.)

Applescript:

on run
   tell application id "com.apple.systemEvents"
       
       set procname to name ofevery application process whose frontmost is true and visible is true
   endtell
   tell application (procname as text)
       activate
       tell application id "com.apple.systemEvents"
           keystroke "c"using command down
       endtell
   endtell
   
   set theUrl tothe clipboard
   set failed to false
   try
       set filename to quoted form of POSIX path of (choose file name)
   onerror
       set failed to true
   endtry
   
   
   ifnot failed then
       do shell script"curl " & theUrl & " -o " & filename
   endif
end run

Edit
It is 10 scripts you can have a short cut key to. That'll hold for my purposes, and spare me some agony, relating to scripting AppleScript from within Automator, besides that, I think I have a home-rolled solution for short-cut keys and scripts.

I really recommend FastScripts for those that doesn't have it. After it is running and properly placed in an application or utilities folder, you fire up AppleScript editor, and hook away the "Show Script menu" in its preferences pane.

Last edited by McUsrII (2013-07-18 04:28:38 am)

The declaration of a variable or property identifier is the first valid occurrence of the identifier in a object. The form and location of the declaration determine how AppleScript treats the identifier in that object.

The scope is the range over which AppleScript recognizes a declared identifier within a object. The scope of a variable depends on where you declare it and whether you declare it as or . The scope of a property extends to the entire object in which it is declared. After declaring a property, you can reuse the same identifier as a separate variable only if you first declare it as a variable.

Lifetime refers to the period of time over which a variable or property is in existence. Only the values of properties and variables can persist after a script is run.

In the discussions that follow, declarations and statements in a object that occur outside of any handlers or nested objects are identified as outside.

The following examples show the four basic forms for declaring variables and properties in AppleScript:

If you want to use the same identifier in several different places in a script, you should either declare it as a property or as a variable.

It is often convenient to limit the scope of a particular identifier to a single handler or nested object, which you can do by defining it as a variable in the handler or object. Outside, the identifier has no value associated with it and can be reused elsewhere in the script. When used this way, a variable is said to shadow (or block access to) a variable or property with the same name, making the global version inaccessible in the scope of the handler or object where the variable is declared.

Scope of Properties and Variables Declared in a Script Object

Table 3-1 shows the scope and lifetime for variables and properties that are declared at the top level in a object (outside any handlers or nested objects).

Declaration type

Scope (visibility)

Lifetime

Everywhere in script

Reset when script is recompiled

Everywhere in script

Reset when reinitialized in script or when script is recompiled

Within handler only

Reset when script is run

Within handler only

Reset when script is run

The scope of a property in a object extends to any subsequent statements anywhere in the script. Consider the following example:

property currentCount : 0
increment()
on increment()
set currentCount to currentCount + 1
display dialog "Count is now " & currentCount & "."
end increment

When it encounters the identifier anywhere in this script, AppleScript associates it with the property.

The value of a property persists after the script in which the property is defined has been run. Thus, the value of is 0 the first time this script is run, 1 the next time it is run, and so on. The property’s current value is saved with the object and is not reset to 0 until the script is recompiled—that is, modified and then run again, saved, or checked for syntax.

The value of a variable also persists after the script in which it is defined has been run. However, depending on how it is initialized, a variable may be reset each time the script is run again. The next example shows how to initialize a variable so that it is initialized only the first time a script is run, and thus produces the same result as using a property in the previous example:

global currentCount
increment()
on increment()
try
set currentCount to currentCount + 1
on error
set currentCount to 1
end try
display dialog "Count is now " & currentCount & "."
end increment

The first time the script is run, the statement generates an error because the variable has not been initialized. When the error occurs, the block initializes . When the script is run again, the variable has already been initialized, so the error branch is not executed, and the variable keeps its previous value. Persistence is accomplished, but not as simply as in the previous example.

If you don’t want the value associated with an identifier to persist after a script is run but you want to use the same identifier throughout a script, declare a variable and use the command to set its value each time the script is run:

global currentCount
set currentCount to 0
on increment()
set currentCount to currentCount + 1
end increment
increment() --result: 1
increment() --result: 2

Each time the handler is called within the script, the variable increases by 1. However, when you run the entire script again, is reset to 0.

In the absence of a variable declaration, the scope of a variable declaration using the command is normally restricted to the handler for the script. For example, this script declares two separate variables:

set currentCount to 10
on increment()
set currentCount to 5
end increment
increment() --result: 5
currentCount --result: 10

The scope of the first variable’s declaration is limited to the handler for the script. Because this script has no explicit handler, outside statements are part of its implicit handler, as described in run Handlers. The scope of the second declaration, within the handler, is limited to that handler. AppleScript keeps track of each variable independently.

To associate a variable in a handler with the same variable declared with the command outside the handler, you can use a declaration in the handler, as shown in the next example. (This approach also works to associate a variable in a nested object.)

set currentCount to 0
on increment()
global currentCount
set currentCount to currentCount + 1
end increment
increment() --result: 1
currentCount --result: 1

To restrict the context of a variable to a script’s handler regardless of subsequent declarations, you must declare it explicitly as a variable, as shown in this example:

local currentCount
set currentCount to 10
on increment()
global currentCount
set currentCount to currentCount + 2
end increment
increment() --error: "The variable currentCount is not defined"

Because the variable in this example is declared as local to the script, and hence to its implicit handler, any subsequent attempt to declare the same variable as results in an error.

If you declare an outside variable with the command and then declare the same identifier as a property, the declaration with the command overrides the property definition. For example, the following script returns 10, not 5. This occurs because AppleScript evaluates property definitions before it evaluates command declarations:

set numClowns to 10 -- evaluated after property definition
property numClowns: 5 -- evaluated first
numClowns --result: 10

The next example, shows how to use a variable declaration in a object to associate a variable with an outside property:

property currentCount : 0
script Paula
property currentCount : 20
script Joe
global currentCount
on increment()
set currentCount to currentCount + 1
return currentCount
end increment
end script
tell Joe to increment()
end script
run Paula --result: 1
run Paula --result: 2
currentCount --result: 2
currentCount of Paula --result: 20

This script declares two separate properties: one outside any handlers (and objects) in the main script and one in the object but outside of any handlers or objects within . Because the script declares the variable , AppleScript looks for at the top level of the script, thus treating Joe’s and at the top level of the script as the same variable.

Scope of Variables Declared in a Handler

A handler can’t declare a property, although it can refer to a property that is declared outside any handler in the object. (A handler can contain script objects, but it can’t contain another handler, except in a contained script object.)

Table 3-2 summarizes the scope of variables declared in a handler. Examples of each form of declaration follow.

Declaration type

Scope (visibility)

Lifetime

Within handler only

Reset when script is recompiled; if initialized in handler, then reset when handler is run

Within handler only

Reset when handler is run

Within handler only

Reset when handler is run

The scope of a variable declared in a handler is limited to that handler, although AppleScript looks beyond the handler when it tries to locate an earlier occurrence of the same variable. Here’s an example:

set currentCount to 10
on increment()
global currentCount
set currentCount to currentCount + 2
end increment
increment() --result: 12
currentCount --result: 12

When AppleScript encounters the variable within the handler, it doesn’t restrict its search for a previous occurrence to that handler but keeps looking until it finds the declaration outside any handler. However, the use of in any subsequent handler in the script is local to that handler unless the handler also explicitly declares as a variable.

The scope of a variable declaration in a handler is limited to that handler, even if the same identifier has been declared as a property outside the handler:

property currentCount : 10
on increment()
local currentCount
set currentCount to 5
end increment
increment() --result: 5
currentCount --result: 10

The scope of a variable declaration using the command in a handler is limited to that handler:

script Henry
set currentCount to 10 -- implicit local variable in script object
on increment()
set currentCount to 5-- implicit local variable in handler
end increment
return currentCount
end script
tell Henry to increment() --result: 5
run Henry --result: 10

The scope of the first declaration of the first variable in the object is limited to the handler for the object (in this case, an implicit handler, consisting of the last two statements in the script). The scope of the second declaration, within the handler, is limited to that handler. The two instances of are independent variables.

0 thoughts on “Applescript Do Shell Script Variable Assignment

Leave a Reply

Your email address will not be published. Required fields are marked *