简体   繁体   English

为什么此小程序似乎无法按预期工作?

[英]Why doesn't this applescript seem to work as expected?

I'm writing a service using automator. 我正在使用自动器编写服务。 It receives no input in any application . 它在any application no input接收no input

All it does is run this simple script: 它所做的全部就是运行以下简单脚本:

on run {input, parameters}
--FIRST BLOCK
    tell application "System Events"
        set app_name to name of the first process whose frontmost is true
    end tell
--SECOND BLOCK
    if (do shell script "defaults read com.apple.finder AppleShowAllFiles") is equal to "0" then
        do shell script "defaults write com.apple.finder AppleShowAllFiles 1"
    else
        do shell script "defaults write com.apple.finder AppleShowAllFiles 0"
    end if
--THIRD BLOCK
    do shell script "killall Finder"
    delay 0.5
--FOURTH BLOCK
    if (app_name is equal to "Finder") then
        tell application "Finder"
            activate
        end tell
    end if
end run

I'll walk you trough it step by step: 我将逐步引导您浏览:

first block: get the name of the current frontmost app and store it in a variable app_name . 第一个块:获取当前最前面的应用程序的名称,并将其存储在变量app_name

second block: toggle the hidden files variable on or of, depending on its value. 第二个块:根据其值在或上切换隐藏文件变量。

third block: run killall Finder to relaunch Finder, taking the toggle from the second block into effect. 第三块:运行killall Finder重新启动Finder,使第二块的切换生效。 Pause 0.5 sec, somehow this is necessary (don't know why, but without this the next instruction will be ignored). 暂停0.5秒,这在某种程度上是有必要的(不知道为什么,但是如果没有这个,则下一条指令将被忽略)。

fourth block: Check what the variable app_name was. 第四块:检查变量app_name是什么。 If it equals Finder this means finder was active when the script was initiated, thus activate Finder once more ( killall Finder leaves it in the background). 如果它等于Finder则意味着在启动脚本时Finder处于活动状态,因此请再次激活Finder( killall Finder将其killall Finder在后台)。

Problem: Everything works as expected but for one thing: when using this service in the Finder, Finder doesn't get activated again. 问题:一切都按预期工作,但有一件事情:在Finder中使用此服务时,Finder不会再次被激活。

One might argue that there must be something wrong with the code in the fourth block, yet I've experimented a bit to show everything works as expected: 有人可能会争辩说第四块中的代码一定存在问题,但是我做了一些实验以显示一切都按预期进行:

When I replace equal by not equal and run the script from any app that is not the Finder, Finder DOES get activated as should be. 当我用not equal替换equal并且从不是Finder的任何应用程序运行脚本时,Finder会被激活。

So it seems that there only is a problem when the script is fired when Finder is in front. 因此,似乎只有在Finder位于前面时才触发脚本时才有问题。

(This is what the service should do: from within any app, toggle the visibility of the hidden files in Finder. When Finder was in front, it should be in front after execution of the cript, when another app was in front, this app should still be in front.) (这是服务应该执行的操作:从任何应用程序中,切换Finder中隐藏文件的可见性。当Finder位于最前面时,它应在执行cript后位于最前面;当另一个应用程序位于最前面时,此应用程序应该仍然在前面。)

I'm on Lion. 我在狮子上

I have come across this before too. 我也曾经遇到过。 Basically you're saying that when this gets run when the Finder is frontmost that the Finder is not really frontmost. 基本上,您说的是当Finder位于最前面时,当它运行时,Finder并不是真正位于最前面。 And that is true because you said this is an automator service. 这是正确的,因为您说这是一个自动化服务。 I believe automator stuff is run by an application named "Automator Runner". 我相信自动化程序的东西是由一个名为“ Automator Runner”的应用程序运行的。 So actually as soon as the service is run Automator Runner becomes frontmost. 因此,实际上,服务一运行,Automator Runner就成为最前端。 Note that it is a faceless application so you can't see that it's frontmost but it is. 请注意,它是一个无界面的应用程序,因此您看不到它是最前端的,但是它是最前端的。 So when you check if the Finder is frontmost it never is. 因此,当您检查Finder是否位于最前面时,它就永远不会位于最前面。 Does that make sense? 那有意义吗? I see the same thing when running applescripts because they're run using Applescript Runner. 运行applescript时,我看到的是同一件事,因为它们是使用Applescript Runner运行的。

So how do you fix this? 那么如何解决这个问题? Here's a thought. 这是一个想法。 Make this your FIRST BLOCK and see if it helps... 将其设置为第一个块,看看是否有帮助...

tell application "System Events"
    set app_name to name of the first process whose frontmost is true
    if app_name is "Automator Runner" then
        set visible of process "Automator Runner" to false
        set app_name to name of first process whose frontmost is true
    end if
end tell

NOTE: I'm not certain that Automator Runner will be the frontmost process. 注意:我不确定Automator Runner将是最前面的过程。 It may be something else like the name of your automator action. 这可能与您的自动操作名称类似。 But you can be certain something else is frontmost because of the automator action being run... so if my code doesn't work then you just have to figure out the name of the process that's running when your automator action is running and put that into the code. 但是您可以确定,由于运行了automator动作,所以其他东西在最前面……因此,如果我的代码不起作用,那么您只需找出在执行automator动作时正在运行的进程的名称,然后进入代码。 You can always put a "display dialog" in your code to show you the name of the frontmost process. 您始终可以在代码中放置一个“显示对话框”,以向您显示最前面的进程的名称。

One other tip. 另一个提示。 In general I do not like to use the KILLALL command if I can use a QUIT command instead. 通常,如果我可以使用QUIT命令代替,则我不喜欢使用KILLALL命令。 Quit is designed for the Mac and makes sure things are stopped gracefully. Quit是为Mac设计的,可确保一切正常停止。 As luck would have it the Finder has a quit command. 幸运的是,Finder具有退出命令。 Try this for your 3rd and 4th blocks. 为您的第3个和第4个块尝试这个。 You'll see that if the Finder was frontmost then we activate it which makes it frontmost again but if it wasn't then we launch it so it is at least running again but doesn't come to the front. 您会看到,如果Finder位于最前面,则我们将其激活,这又使其再次位于最前面,但是如果不是,则我们将其启动,因此它至少再次运行但没有出现在最前面。

-- quit the Finder
tell application "Finder" to quit

-- delay until the Finder quits
repeat
    try
        tell application "System Events" to get first process whose name is "Finder"
        delay 0.1
    on error
        exit repeat
    end try
end repeat

-- restart the Finder
if (app_name is equal to "Finder") then
    tell application "Finder" to activate
else
    tell application "Finder" to launch
end if

EDIT: It seems your step #2 is incorrect. 编辑:看来您的步骤2不正确。 You need to use "ON" or "OFF" instead of 0 or 1. Try this... 您需要使用“ ON”或“ OFF”而不是0或1。

if (do shell script "defaults read com.apple.finder AppleShowAllFiles") is equal to "ON" then
    do shell script "defaults write com.apple.finder AppleShowAllFiles OFF"
else
    do shell script "defaults write com.apple.finder AppleShowAllFiles ON"
end if

A slight modification of @regulus6633's script works on my machine: @ regulus6633脚本的略微修改可在我的机器上运行:

-- restart the Finder
tell application "Finder" to launch      # always launch
if (app_name is equal to "Finder") then
  tell application "Finder" to activate  # activate separately
end if

I must admit I am not entirely sure of the why and how – the AppleScript documentation for the launch and activate commands does not seem to be adequate in Finder's case… 我必须承认,我并不完全确定为什么以及如何– 启动激活命令的AppleScript文档在Finder的情况下似乎还不够……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 recalculateKeyViewLoop不起作用 - recalculateKeyViewLoop doesn't work 全新安装Lion后,我的应用似乎没有在会话中存储任何内容 - After doing a fresh install of Lion my app doesn't seem to be storing anything in the session gem install pg在OSX Lion上不起作用 - gem install pg doesn't work on OSX Lion OpenGLScreenSnapshot在Mac OS X 10.7 - Lion中不起作用 - OpenGLScreenSnapshot doesn't work in Mac OS X 10.7 - Lion 为什么xlwings无法在Mac OS 10.7.5上正确安装? - Why doesn't xlwings install correctly on Mac OS 10.7.5? 第一个小部件在Dashcode中不起作用,但在部署时可以工作。 我可以解决这个问题吗? - First widget doesn't work in Dashcode but does work when deployed. Can I fix this? 为什么可编辑的可可文本字段单元格不绘制阴影? - Why doesn't an editable Cocoa text field cell draw a shadow? Applescript更改:复制结果列表不一样 - Applescript changes : copy the result as list isn't the same Rails + Mac OS X:libMagickCore-Q16.7.dylib不起作用 - Rails + Mac OS X: libMagickCore-Q16.7.dylib doesn't work 似乎无法更改 OSX Lion 终端中的默认编辑器 - Can't seem to change default editor in OSX Lion terminal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM