2011-05-26 120 views
0

我编写了我的applescript应用程序来隐藏我的无线网卡的窗口。 我遇到了一些问题,如果检查窗口是可见或不可见(避免命令+ H按键没有影响),所以我决定用delay 15使(不是全部)确认窗口弹出。如果我从编辑器运行脚本或双击应用程序文件,它可以工作,但如果我将它设置为从用户登录开始(在设置>帐户>登录元素下),它不起作用! 我试图改变复选框中Save as...页的AppleScript编辑:我尝试都设置为only execute,但任何变化。 与start screen选项实际上它的工作原理,但它问我一个确认,我不希望它(我会优先按cmd + h代替)。 任何人都可以解释为什么我有这个问题?Applescript在OSX启动时无法运行

tell application "System Events" 
set progList to (name of every process) 
set cond to false 
repeat while cond is false 
    if (progList contains "WirelessUtilityCardbusPCI") is true then 
     delay 5 
     activate application "WirelessUtilityCardbusPCI.app" 
     tell application "System Events" to keystroke "h" using [command down] 
     set cond to true 
    else 
     delay 5 
     set progList to (name of every process) 
    end if 
end repeat 
end tell 

编辑:现在它似乎工作!我忘了重新编号set progList to (name of every process)。现在代码是正确的。

+0

需要看到的代码。 – regulus6633 2011-05-26 16:26:19

+0

对不起,但我不认为代码可以改变一些东西。 – Paciotti 2011-05-27 09:09:01

回答

2

我看到你的代码正在工作。那很棒。不过,我发布这个来帮助你学习。我发现你的代码有一些小问题。例如在你的重复循环中,你告诉系统事件按键“h”。没有必要告诉系统事件在那条线上做,因为你已经在系统事件中告诉代码块,所以系统事件已经知道要这样做。

这是我会怎么写你的代码。这不需要击键,这总是一件好事,而且效率更高一些。它的工作原理是,如果进程不存在,则将“设置进程”设置为行错误,然后代码进入“错误”部分以延迟5,然后重复循环尝试再次找到进程。如果找到该进程,则它设置其隐藏它的可见属性。

它也有一个超时机制来防止脚本运行下去。如果你喜欢,请使用这个。祝你好运。

set processName to "WirelessUtilityCardbusPCI" 
set maxTime to 180 -- we only check for 3 minutes, then end 

set inTime to current date 
repeat 
    try 
     tell application "System Events" 
      set theProcess to first process whose name is processName 
      set visible of theProcess to false 
     end tell 
     exit repeat 
    on error 
     if (current date) - inTime is greater than maxTime then 
      tell me 
       activate 
       display dialog "The process " & processName & " could not be found!" buttons {"OK"} default button 1 with icon 0 
      end tell 
      exit repeat 
     end if 
     delay 5 
    end try 
end repeat 

编辑:我已经检查使用TextEdit应用程序上面的代码,它工作正常。要检查它与您的应用程序运行以下。确保运行此代码时应用程序正在运行。如果有错误,将显示它。如果没有错误,将显示2个对话框显示进度。报告你发现的内容。

set processName to "WirelessUtilityCardbusPCI" 

try 
    tell application "System Events" 
     set theProcess to first process whose name is processName 
     display dialog "I have found the process" 
     set visible of theProcess to false 
     display dialog "I just performed the \"set visible\" code" 
    end tell 
on error theError number errorNumber 
    tell me 
     activate 
     display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop 
     return 
    end tell 
end try 
+0

到底什么是“第一”呢?你的代码是非常有趣的,但它不起作用:它没有错误对话框终止,也没有隐藏应用程序。我认为这个过程对于“设置可见”有一些问题,但是这次它不会给我任何错误(不像我以前的尝试)! – Paciotti 2011-05-31 18:45:48

+0

我在上面的编辑部分添加了一些代码,以帮助您找到问题。尝试一下并回报。 – regulus6633 2011-05-31 19:56:13

+0

我不知道为什么,但“设置可见”不适用于我的应用程序!第二个对话框出现,但应用程序仍然可见。 我还有另外一个问题:如果我查找过程,有时我的应用程序在窗口未绘制时运行。通过这种方式,cmd + h按键在没有任何操作的情况下执行,并且脚本终止无效。 – Paciotti 2011-05-31 21:17:23

0

我已经使用登录项目在成功登录时启动AppleScript小程序,所以我的第一个建议是确保它的不是正在运行。让它显示一个自定义对话框或嘟嘟声或类似的东西来确认它是否正在运行。

除此之外,我不知道要提供什么建议,除非你想发布你的脚本执行代码。