AppleScript:为什么这个脚本会启动Safari?

时间:2015-01-13 19:58:27

标签: google-chrome safari applescript automator

这看起来很简单。 " if(frontApp =" Safari")然后"做Safari部分。如果没有,请执行else if部分"否则if(frontApp =" Google Chrome")然后"

当我打开Chrome并运行此脚本(在Automator工作流程中)时,它会正常运行Chrome部分,然后继续打开Safari。

此代码的哪一部分告诉Safari打开,如何阻止这种情况发生?如果只有Chrome打开并且我在Chrome中运行它,我就不想要并且不需要Safari来打开。

提前致谢...

on run {input, parameters}

tell application "System Events" to set frontApp to name of first process whose frontmost is true

if (frontApp = "Safari") then
    using terms from application "Safari"
        tell application frontApp to set currentTabUrl to URL of front document
        tell application frontApp to set currentTabTitle to name of front document
    end using terms from
else if (frontApp = "Google Chrome") then
    using terms from application "Google Chrome"
        tell application frontApp to set currentTabUrl to URL of active tab of front window
        tell application frontApp to set currentTabTitle to title of active tab of front window
    end using terms from
else
    return "You need a supported browser as your frontmost app"
end if

set myInfo to ("MUSIC - " & currentTabUrl & " - " & currentTabTitle)
return myInfo

结束

2 个答案:

答案 0 :(得分:1)

“在应用程序中使用术语'Safari'”行将在首次编译时启动应用程序,或者在运行时打开脚本时首次运行。 我建议在运行脚本对象中放置tell块,除非被调用,否则不会启动:

tell application "System Events" to set frontApp to name of first process whose frontmost is true

if (frontApp = "Safari") then
    set currentTabUrl to run script "tell application \"Safari\" to get URL of front document"
    set currentTabTitle to run script "tell application \"Safari\" to get name of front document"
else if (frontApp = "Google Chrome") then...

答案 1 :(得分:0)

运行脚本是一个相当昂贵的命令,因此您可以使用此运行脚本来优化代码。

set {currentTabTitle, currentTabUrl} to run script "tell application \"Safari\"
tell current tab of its front window 
return { name, url }
end tell
end tell"