2013-01-16 50 views
0

我試圖函數replaceChars在下面的腳本,但得到以下錯誤:問題使用AppleScript功能

error "Finder got an error: Can’t continue replace_chars." number -1708 

的目的是添加腳本來尋人的按鈕,這樣我可以簡單地點擊它將路徑複製到我的剪貼板。我添加文件://本地主機/這樣,當與用戶通過電子郵件作爲直接鏈接到一個文件夾在局域網上共享然後可以使用的鏈接。如果可能的話,我還想爲Windows機器添加剪貼板。

如果你能提供任何指導,它上面將是非常讚賞的任務,這是我在使用AppleScript編程第一次嘗試,所以我不認爲有見地的事情是如何完成的。

繼承人的代碼:

on appIsRunning(appName) 
    tell application "System Events" 
     set isRunning to ((application processes whose (name is equal to appName)) count) 
    end tell 

    if isRunning is greater than 0 then 
     return true 
    else 
     return false 
    end if 
end appIsRunning 

on replace_chars(this_text, search_string, replacement_string) 
    set AppleScript's text item delimiters to the search_string 
    set the item_list to every text item of this_text 
    set AppleScript's text item delimiters to the replacement_string 
    set this_text to the item_list as string 
    set AppleScript's text item delimiters to "" 
    return this_text 
end replace_chars 

if appIsRunning("Finder") then 
    tell application "Finder" 
     set thePath to "file://localhost/" as text 
     set theTarget to (target of front Finder window) as text 
     set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text 
    end tell 
end if 
+0

我的回答解決了'replace_chars'的問題,但不是整體的方法你已經採取了。一個簡短的批評:路徑格式之間的轉換是更好地與[POSIX路徑]完成(http://macscripter.net/viewtopic.php?id=24456)。如果腳本是Finder中的按鈕,那麼檢查它是否正在運行沒有任何用處。你可以很容易地把它變成一個雙行腳本。 –

+0

您的代碼和接受的答案是不正確建立的路徑。正如Michael在他的評論中指出的那樣,我在我的回答中指出,使用posix路徑而不是重建hfs路徑。 [指定路徑(https://developer.apple.com/library/mac/#documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW28) – adayzdone

回答

3

的AppleScript正在尋找在Finder中的腳本字典中的處理程序。您可以使它成爲my replace_chars有AS看看腳本,或(可能更好)移動set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text線從tell塊乾脆。

2

你可以把它變成一個行腳本:

tell application "Finder" to set the clipboard to "file://localhost" & (target of front Finder window as text)'s POSIX path 
+1

我因爲posix和hfs路徑之間的區別不僅僅是路徑分隔符,所以請投票選舉一個。與其他卷一起使用時,所有其他示例都會失敗。 –

0

replace_chars(theTarget, ":", "/") of me更換您的通話replace_chars(theTarget, ":", "/")

set the clipboard to thePath & replace_chars(theTarget, ":", "/") of me as text