使用AppleScript显示IF状态
我正试图根据当前的使用AppleScript显示IF状态,applescript,Applescript,我正试图根据当前的“打开”应用程序(屏幕上最前面的应用程序)来选择不同的事件。我已经使用变量保存了应用程序的名称。使用此代码 tell application "System Events" item 1 of (get name of processes whose frontmost is true) set openWindow to (get name of processes whose frontmost is true) do shell script
“打开”
应用程序(屏幕上最前面的应用程序)来选择不同的事件。我已经使用变量保存了应用程序的名称。使用此代码
tell application "System Events"
item 1 of (get name of processes whose frontmost is true)
set openWindow to (get name of processes whose frontmost is true)
do shell script "echo " & openWindow & " > /Users/name/temp/currentWindow.txt"
end tell
然后,我尝试使用此代码为每个打开的应用程序执行不同的事件
tell application "System Events"
if openWindow = "AppleScript Editor" then
display dialog "my variable: " & openWindow
end if
end tell
然而,这段代码似乎没有做任何事情,我没有任何错误消息或任何东西,但代码没有显示对话框。如果我将对话框的代码放在代码的第一部分,它将显示打开的应用程序的名称
如果您有任何关于如何实现此功能的想法,将非常有帮助在第一个脚本中将openWindow变量转换为字符串:
set openWindow to (get name of processes whose frontmost is true) as string
为了解释你的问题,这是因为这个代码
set openWindow to (get name of processes whose frontmost is true)
该代码返回一个项目列表。请注意,您请求进程(复数),因此可以获得多个进程,因此无论找到的是一个还是多个项目,applescript都会将其作为列表提供给您。奇怪的是,在上面这一行中,您确实要求列表中的“第1项”,但出于某种原因,您没有对这一行代码执行任何操作。通常我会这样写那行代码,所以我只得到第1项
set openWindow to name of first process whose frontmost is true
无论如何,您不能将字符串“AppleScript Editor”与列表{“AppleScript Editor”}进行比较。它们不相等,因此您的if语句永远不会为真。
“显示”对话框显示一个字符串。因此,当您将代码移到if语句之外时,applescript足够智能,可以将列表转换为字符串,以便显示
所以,底线是您将获得一个列表,并且您必须访问列表中的项目。这些项是字符串,因此获取一个(在您的示例中,您需要项1)并在if语句中使用它
希望你能从这个解释中学习。祝你们好运。谢谢大家,这非常有帮助。