How can Applescript objects be referenced before they are defined?

Does anyone know why this applescript works? I do not understand why. The script creates three dialog boxes containing the same message: "Hello there." I have two questions:

1) How can I set j and k for the i reference before I am defined?

2) Why does r not refer to i defined in test2?

on test1()
  return get a reference to i
end test

on run
  set j to test1()
  set k to a reference to i
  set i to "Hi there"
  display dialog j
  display dialog k
  test2()
end run

on test2()
  set i to "now see here"
  set r to a reference to i
  display dialog r
end test2

Note. The script editor is version 2.7 and the version of AppleScript is 2.4.

+1
source share
4 answers
  • You can create references to the property of an object or the [s] element or global variables (annoying erroneous functions that behave like poorly designed properties), rather than local variables. For example, all these works:

    on test2()
        set x to {i:"now see here"}
        set r to a reference to i of x
        display dialog r
    end test2
    
    test2()
    
    on test3()
        set x to {"now see here"}
        set r to a reference to item 1 of x
        display dialog r
    end test3
    
    test3()
    
    on test4()
        script x
            property i : "now see here"
        end script
        set r to a reference to i of x
        display dialog r
    end test4
    
    test4()
    
    
    property i : "now see here"
    
    on test5()
        set r to a reference to i
        display dialog r
    end test4
    
    test5()
    
  • run ( ), local. - , run, .

, - . : , C-.

+1

. , . , , , , . .

  • , , .

  • test2 , , test2.

script. , ?

0

, foo 18 . , , Object Specifiers. , , of (get me).

return get a reference to i

, .

return get a reference to i of (get me)

, . a reference to get , . i of. , get, , . me, script. i , test1. display dialog j, . i. k, .

, , , , . , , , AppleScript , .

get . . ( . , , )

1:

set r to a reference to i

set r to get a reference to i of (get me)

2:

set r to a reference to item 2 of i

set r to get a reference to item 2 of (get i of me)

3:

set r to a reference to i of me

set r to get a reference to i of (get me) -- same as Example 1

4:

set r to a reference to item 2 of i of me

set r to get a reference to item 2 of i of (get me) -- differs from Example 2

, . in of , , . script. .

property j : {1, 2, {3, 4}, {{5, {6, 7}}, 8, 9}}

log "Line 1: " & j
set r to a reference to item 3 of j -- phrase is "item 3 of", object is (get j of me)
set contents of r to "aa" -- Succeeds since total count is 1
log "Line 2: " & j

set r to a reference to item 2 of (get item 1 of item 4 of j) -- phrase is "item 2 of", object is (get item 1 of item 4 of (get j of me)) 
set contents of r to "bb" -- Succeeds since total count is 1
log "Line 3: " & j

set j to {1, 2, {3, 4}, {{5, {6, 7}}, 8, 9}}
log "Line 4: " & j
set r to a reference to item 3 of j of me -- phrase is "item 3 of j of", object is (get me)
try
    set contents of r to "cc"  -- Fails since total count is 2 
on error msg
    log "Line 5: " & msg
end try
log "Line 6: " & j

set r to a reference to item 2 of item 1 of item 4 of j -- phrase is "item 2 of item 1 of item 4 of", object is (get j of me) 
try
    set contents of r to "dd" -- Fails since total count  is 3
on error msg
    log "Line 7: " & msg
end try
log "Line 8: " & j

.

(*Line 1: 123456789*)
(*Line 2: 12aa56789*)
(*Line 3: 12aa5bb89*)
(*Line 4: 123456789*)
(*Line 5: Can’t set item 3 of j to "cc".*)
(*Line 6: 123456789*)
(*Line 7: Can’t set item 2 of item 1 of item 4 of {1, 2, {3, 4}, {{5, {6, 7}}, 8, 9}} to "dd".*)
(*Line 8: 123456789*)

set contents of r to "bb" , get . .

" , , [ ]?" 17 .

. .

set r to missing value
set s to missing value
FirstAndLast(a reference to r, a reference to s, "now is the time for all good men")
log r
log s

on FirstAndLast(alpha as reference, omega as reference, message as text)
    set contents of alpha to first word of message
    set contents of omega to last word of message
end FirstAndLast

:

(*now*)
(*men*)

, , : 1) r s . 2) r s FirstAndLast. Applescript - , , . - , AppleScript. .

set {r, s} to FirstAndLast("now is the time for all good men")
log r
log s

on FirstAndLast(message as text)
    set alpha to first word of message
    set omega to last word of message
    return {alpha, omega}
end FirstAndLast 

. 1) . 2) r s , . 3) r s , FirstAndLast.

, , - . , , , .

on test()
    log class of test as text
end test
property y : test
set x to test
log class of x as text
log class of y as text
x()
y()
0

This is not an answer to the original question. This is in response to a foos comment made on December 1st. This is an example of how to create two instances of the same boy script that has a common record. Also, learning from foo's comment, I used the entry to share properties with all instances of the script. I don't know if this is the best way to do this, but at least there are no global variables.

script mom
    property hair : "blond"
    property shared : {address:"12 walnut st", phone:"555-1234"}
end script

script boy
    property parent : mom
end script

script girl
    property parent : mom
end script

on clone from old given shared:sharedSwitch as boolean : false
    if sharedSwitch then
        set shared to shared of old
        set shared of old to missing value
    end if
    copy old to new
    if sharedSwitch then
        set shared of new to shared
        set shared of old to shared
    end if
    return new
end clone

property myboy : clone from boy with shared

on run
    log "mom hair is" & tab & tab & hair of mom
    log "boy hair is " & tab & tab & hair of boy
    log "girl hair is " & tab & tab & hair of girl
    log "myboy hair is " & tab & hair of myboy
    set hair of boy to "redhead"
    log "**Set boy hair to redhead**"
    log "mom hair is" & tab & tab & hair of mom
    log "boy hair is " & tab & tab & hair of boy
    log "girl hair is " & tab & tab & hair of girl
    log "myboy hair is " & tab & hair of myboy
    set hair of myboy to "flattop"
    log "**Set myboy hair to flattop**"
    log "mom hair is" & tab & tab & hair of mom
    log "boy hair is " & tab & tab & hair of boy
    log "girl hair is " & tab & tab & hair of girl
    log "myboy hair is " & tab & hair of myboy
    log
    log "mom phone is" & tab & phone of shared of mom
    log "boy phone is" & tab & phone of shared of boy
    log "girl phone is" & tab & phone of shared of girl
    log "myboy phone is" & tab & phone of shared of myboy
    set phone of shared of myboy to "555-9999"
    log "**Set myboy phone to 555-9999**"
    log "mom phone is" & tab & phone of shared of mom
    log "boy phone is" & tab & phone of shared of boy
    log "girl phone is" & tab & phone of shared of girl
    log "myboy phone is" & tab & phone of shared of myboy
end run
0
source

All Articles