2013-10-13 53 views
0

我使用下面的代碼在谷歌查詢在applescript中打開youtube,因爲新的谷歌不會將我的搜索字符串帶到youtube.Unable在Firefox中打開url與谷歌搜索字符串Firefox瀏覽器。我非常感謝任何幫助。提前感謝。applescript在YouTube上打開谷歌搜索查詢

在這裏,我得到一個錯誤:

tell application "Firefox" to activate 
tell application "System Events" 
    keystroke "l" using command down 
    keystroke "c" using command down 

    set tr to get the clipboard 


end tell 



return trimgoogle(tr) 
on trimgoogle(sourceAddress) 
    set AppleScript's text item delimiters to {"#"} 
    set addressParts to (every text item in sourceAddress) as list 
    set AppleScript's text item delimiters to "" 
    set nameOnly to item 2 of addressParts 

    set AppleScript's text item delimiters to {"&"} 
    set addressParts2 to (every text item in nameOnly) as list 
    set AppleScript's text item delimiters to "" 
    set nameOnly1 to item 1 of addressParts2 
    set nameOnly2 to nameOnly1 


    set AppleScript's text item delimiters to {"="} 
    set addressParts21 to (every text item in nameOnly2) as list 
    set AppleScript's text item delimiters to "" 
    set nameOnly12 to item 2 of addressParts21 


    set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12 

    return newurl 

end trimgoogle 

tell application "Firefox" 
OpenURL newurl 
end tell 

回答

1

試試這個:

tell application "Firefox" 
    open location newurl 
end tell 

編輯:

Firefox不會自動激活自身,所以我只是嘗試這樣做:

tell application "Firefox" 
    activate 
    open location "http://www.wikipedia.com/" 
end tell 

w這裏工作。

這裏更多有關編碼的文本: http://www.macosxautomation.com/applescript/sbrt/sbrt-08.html

EDIT2: OK,我明白你的意思,這將可以解決這樣的:

tell application "Firefox" to activate 
tell application "System Events" 
    keystroke "l" using command down 
    keystroke "c" using command down 
    set tr to get the clipboard 
end tell 

set newurl to my trimgoogle(tr) 

tell application "Firefox" 
    activate 
    open location newurl 
end tell 

on trimgoogle(sourceAddress) 
    set AppleScript's text item delimiters to {"#"} 
    set addressParts to (every text item in sourceAddress) as list 
    set AppleScript's text item delimiters to "" 
    set nameOnly to item 2 of addressParts 

    set AppleScript's text item delimiters to {"&"} 
    set addressParts2 to (every text item in nameOnly) as list 
    set AppleScript's text item delimiters to "" 
    set nameOnly1 to item 1 of addressParts2 
    set nameOnly2 to nameOnly1 

    set AppleScript's text item delimiters to {"="} 
    set addressParts21 to (every text item in nameOnly2) as list 
    set AppleScript's text item delimiters to "" 
    set nameOnly12 to item 2 of addressParts21 

    set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12 

    return newurl 

end trimgoogle 
+0

我想,也不過它什麼也不做!代碼中沒有錯誤,但是新窗口(選項卡未打開)。 – jason

+0

嘗試使用硬編碼的網址。如果它工作,你必須編碼你的網址(在這種情況下,「nameOnly12」的內容) - 我只是在OS 10.8.2上再次嘗試使用最新的Firefox,它打開一個有效的位置 – 2013-10-14 08:48:13

+0

使newurl全球化,它開始工作。你可以告訴我任何其他方式來做到這一點,而不是使紐羅爾全球? – jason