2010-08-25 33 views
1

我對AppleScript引用感到困惑......我幾乎從不在AppleScript中開發,並且在AppleScript如何處理引用時遇到了很難找到的好文檔。同樣的代碼,但是我運行它run處理程序中,而不是foo處理程序 -爲什麼AppleScript不能在此測試代碼中將hash的firstValue轉換爲類型引用?

on run 
    foo() 
end run 

on foo() 
    set the hash to {firstValue:1, secondValue:2} 
    set the hashRef to a reference to the hash 
    return the firstValue of hashRef 
end foo 

但是,下面的代碼工作::爲什麼

on run 
    set the hash to {firstValue:1, secondValue:2} 
    set the hashRef to a reference to the hash 
    return the firstValue of hashRef 
end run 

下面的代碼失敗,因爲AppleScript的Can’t make firstValue of hash into type reference.第一個代碼示例在第二個代碼示例工作時失敗?更好的問題,有人可以指示我解釋這個文件的方向,所以我可以知道我的錯誤是什麼?

編輯:菲利普的答案指出我正確的方向,現在我看到了什麼使我困惑。 The official AppleScript docs指出「AppleScript通過引用傳遞所有參數,這意味着處理程序和調用者之間共享一個傳遞的變量,就好像該處理程序使用set命令創建了一個變量」。 但是,這並不意味着AppleScript傳遞一個AppleScript參考對象作爲每個參數!

這裏的下面,更詳細的,代碼示例,展示我公司開發的最終工作的解決方案:

on run 
    foo() 
end run 

on isRef(someValue) 
    try 
     someValue as reference 
     return true 
    on error 
     return false 
    end try 
end isRef 

on foo() 
    log "In foo()" 
    set the objectList to makeObjectList() 
    log "objectList isRef =" & isRef(objectList) 
    set theObject to makeObject given id:0, name:"Test" 
    addObjectToObjectList(theObject, objectList) 
    log "foo(): object name =" & name of theObject 
    log item 1 of allItems of objectList 
    log item 1 of goodItems of objectList 
    set the name of item 1 of allItems of objectList to "Test3" 
    log item 1 of allItems of objectList 
    log item 1 of goodItems of objectList 
end foo 

on makeObjectList() 
    set the hash to {allItems:{}, goodItems:{}, badItems:{}} 
    return the hash 
end makeObjectList 

on makeObject given name:theName, id:theId 
    set theObject to {name:theName, id:theId} 
    return theObject 
end makeObject 

on addObjectToObjectList(object, objectList) 
    log "In addObjectToObjectList" 
    log "object isRef =" & isRef(object) 
    copy object to the end of allItems of the objectList 
    set objectRef to a reference to the last item in allItems of the objectList 

    set name of objectRef to "Test2" 
    log "object name =" & name of object 


    log "objectRef isRef =" & isRef(objectRef) 
    log "objectRef name =" & name of (contents of objectRef) 
    copy objectRef to the end of goodItems of the objectList 
end addObjectToObjectList 

,它的輸出如下:

 
(*In foo()*) 
(*objectList isRef =false*) 
(*In addObjectToObjectList*) 
(*object isRef =false*) 
(*object name =Test*) 
(*objectRef isRef =true*) 
(*objectRef name =Test2*) 
(*foo(): object name =Test*) 
(*name:Test2, id:0*) 
(*name:Test2, id:0*) 
(*name:Test3, id:0*) 
(*name:Test3, id:0*) 

問題的關鍵是,我不能在處理程序中引用局部變量 - 但只要這些引用存儲回該記錄中,我就可以引用記錄的某些部分,這是我之後的功能。

我懷疑任何人都不會讀到這裏,下到這個問題:-)

+0

你想要做什麼,需要參考?我只問,因爲我一直在Applescript工作多年,而且我從來沒有需要參考,特別是在如此簡單的情況下。您不需要創建對記錄的引用來訪問請求的值。我對引用的基本理解是,它與指針類似。 – 2010-08-25 20:26:52

+0

@Philip - 我不認爲我需要引用,但我的代碼似乎沒有工作沒有他們...我(試圖)做的是在內存中維護一個複雜的數據結構,並將它傳遞給各種處理程序來處理它。我還讀到使用引用速度要快得多... – Josh 2010-08-25 20:28:45

+0

@Philip:請參閱[Apple's Docs](http://developer.apple.com/mac/library/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html# // apple_ref/doc/uid/TP40000983-CH1g-BBCDBHIE),特別是關於「對於大型列表,在將大量項目插入列表時使用對運算符的引用更有效」的部分。我會看看我是否可以讓我的代碼無w/o裁判工作。 – Josh 2010-08-25 20:37:04

回答

0

我真的認爲這是一個範圍問題在這裏。如果我在foo()之內聲明瞭對Finder和InDesign中選擇的引用,從而獲取作品中包含的引用和值。但是由於您不是在特定的應用程序中工作,目標必須在腳本的範圍內。瞭解Applescript及其傳奇般的won-可能是使用參考算子的「正確」方式。

我是能夠使這項工作,如果我做了哈希稿件的全球property,如下所示:property hash : {firstValue:1, secondValue:2}.當時我能夠從foo()成功調用值。完整的示例代碼:

property hash : {firstValue:1, secondValue:2} 

on run 
    foo() 
end run 

on foo() 
    set the hashRef to a reference to the hash 
    return the firstValue of hashRef 
end foo 

不是特定的問題答案,而是一個會讓你度過一天的問題。

+0

謝謝@Philip!當我繼續調試我的腳本時,我現在遇到了一個更復雜的參考問題,但您應該接受這個問題。如果我無法弄清我更復雜的問題,我會發佈一個新問題。 – Josh 2010-08-25 21:11:28

+0

我現在看到什麼讓我困惑。我將編輯我的答案併發布一些額外的細節,但顯然讓我困惑的是「AppleScript通過引用傳遞所有參數*」,但這不會**意味着與「AppleScript傳遞參考所有參數「。換句話說,AppleScript中有兩種引用! – Josh 2010-08-25 21:26:52

+0

如果你想讓你的大腦受傷,請閱讀我編輯的問題。我想我現在要休息一天了,哈! – Josh 2010-08-25 21:36:28