【问题标题】:AppleScript: Why does this script launch Safari?AppleScript:为什么这个脚本会启动 Safari?
【发布时间】:2015-03-11 21:37:31
【问题描述】:

这看起来很简单。 "if (frontApp = "Safari") then" 做 Safari 部分。如果不是,则执行 else if 部分“else if (frontApp = "Google Chrome") then"

当我打开 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

结束运行

【问题讨论】:

    标签: google-chrome safari applescript automator


    【解决方案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...
    

    【讨论】:

    • 好的,这似乎成功了。我不认为我会自己想出那个。谢谢jweaks!
    • 我不得不用谷歌搜索来了解如何接受答案。不是很明显...
    【解决方案2】:

    run script 是一个相当昂贵的命令,所以你可以用这个 run script 稍微优化一下代码。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多