Applescript未使用用户输入执行
我有一个简单的Applescript,它输入电话号码和一条标准消息。当我硬编码一个电话号码时,脚本按预期运行。我尝试扩展它以接受用户输入,它现在提供了一个已完成的工作流,但消息没有被击键。 在Automator窗口中运行时,它可以按预期工作,但这是它唯一可以工作的地方 最终目标:能够在任何应用程序中运行此Applescript,并且消息被击键Applescript未使用用户输入执行,applescript,Applescript,我有一个简单的Applescript,它输入电话号码和一条标准消息。当我硬编码一个电话号码时,脚本按预期运行。我尝试扩展它以接受用户输入,它现在提供了一个已完成的工作流,但消息没有被击键。 在Automator窗口中运行时,它可以按预期工作,但这是它唯一可以工作的地方 最终目标:能够在任何应用程序中运行此Applescript,并且消息被击键 set phoneNumber to "error" set contactName to (choose from list {"John", "Bob
set phoneNumber to "error"
set contactName to (choose from list {"John", "Bob", "Jill"} with prompt "Who do you want to contact?") as string
if contactName is "John" then
set phoneNumber to "(310) 213-1234"
else if contactName is "Bob" then
set phoneNumber to "(213) 123-1234"
else if contactName is "Jill" then
set phoneNumber to "(424) 456-7890"
end if
set theMessage to "Hi, please contact " & contactName & " at: " & phoneNumber & return & "Thank you."
tell application "System Events"
keystroke theMessage
end tell
下面是一个示例,说明如何在运行AppleScript操作中将其编码为自动程序服务:
on run {input, parameters}
tell application "System Events"
-- # Get the name of the App that has focus when this Service is triggered.
set activeApp to name of first application process whose frontmost is true
delay 0.5
end tell
tell current application
activate -- # Set focus back to me so the list box is frontmost.
set contactName to (choose from list {"John", "Bob", "Jill"} with prompt "Who do you want to contact?") as string
if return is false then return -- # User clicked Cancel or pressed Esc, exit the run handler.
if contactName is "John" then
set phoneNumber to "(310) 213-1234"
else if contactName is "Bob" then
set phoneNumber to "(213) 123-1234"
else if contactName is "Jill" then
set phoneNumber to "(424) 456-7890"
end if
set the clipboard to "Hi, please contact " & contactName & " at: " & phoneNumber & linefeed & "Thank you."
end tell
-- # Set focus back to the App that was frontmost when the Service was triggered.
tell application activeApp
activate
delay 1 -- # Make sure the App has come frontmost before pasting what's on the clipboard.
tell application "System Events"
try
-- # Insure the front window is indeed active.
perform action "AXRaise" of window 1 of application process activeApp
delay 0.2
end try
try
-- # Paste from the clipboard.
keystroke "v" using command down
delay 0.2
end try
end tell
end tell
-- # Clear the clipboard.
tell current application to set the clipboard to ""
end run
注意:延迟命令可能是必需的,也可能不是必需的,或者可能需要为您的系统调整其值。您可以随时删除那些您认为不需要的内容,但我觉得它们位于战略位置,会保留下来。下面是一个示例,说明如何在运行AppleScript操作中将其编码为一个自动程序服务:
on run {input, parameters}
tell application "System Events"
-- # Get the name of the App that has focus when this Service is triggered.
set activeApp to name of first application process whose frontmost is true
delay 0.5
end tell
tell current application
activate -- # Set focus back to me so the list box is frontmost.
set contactName to (choose from list {"John", "Bob", "Jill"} with prompt "Who do you want to contact?") as string
if return is false then return -- # User clicked Cancel or pressed Esc, exit the run handler.
if contactName is "John" then
set phoneNumber to "(310) 213-1234"
else if contactName is "Bob" then
set phoneNumber to "(213) 123-1234"
else if contactName is "Jill" then
set phoneNumber to "(424) 456-7890"
end if
set the clipboard to "Hi, please contact " & contactName & " at: " & phoneNumber & linefeed & "Thank you."
end tell
-- # Set focus back to the App that was frontmost when the Service was triggered.
tell application activeApp
activate
delay 1 -- # Make sure the App has come frontmost before pasting what's on the clipboard.
tell application "System Events"
try
-- # Insure the front window is indeed active.
perform action "AXRaise" of window 1 of application process activeApp
delay 0.2
end try
try
-- # Paste from the clipboard.
keystroke "v" using command down
delay 0.2
end try
end tell
end tell
-- # Clear the clipboard.
tell current application to set the clipboard to ""
end run
注意:延迟命令可能是必需的,也可能不是必需的,或者可能需要为您的系统调整其值。您可以随时删除那些您认为不需要的内容,但我觉得它们位于战略位置,将保留它们。您缺少
set contactName…
代码行中的右括号。另外,如果在set contactName…
代码行的末尾只需要一个字符串,则会出现不必要的括号和5次as string
。也就是说,如果您没有将焦点设置为自动操作之外的文本字段,那么您希望击键
命令的输出将转到哪里?我建议你澄清你想做什么。谢谢!添加了缺少的括号,我有我的applescript,但在重新输入时输入错误。欣赏更干净的版本。顺便说一句,示例代码在设置contactName…
代码行周围不包含任何错误处理,如果选择了取消,则应如此。还有,现在还不清楚你到底想做什么,我重复一遍。。。“这就是说,如果您没有将焦点设置为自动操作之外的文本字段,那么您希望击键命令的输出转到何处?我建议您澄清您试图执行的操作。”我希望短脚本在聚焦输入字段后运行。我在多个地方使用此消息,并且输入字段并不总是在页面上的同一位置。有时在网页上,有时在电子邮件中。我将其设置为宏,以便在需要输入消息的任何位置运行它。工作流报告消息已完成,但消息从未被击键。我不明白为什么它运行时没有预期的“设置contactName”部分。您缺少设置contactName…
代码行的右括号。另外,如果在set contactName…
代码行的末尾只需要一个字符串,则会出现不必要的括号和5次as string
。也就是说,如果您没有将焦点设置为自动操作之外的文本字段,那么您希望击键
命令的输出将转到哪里?我建议你澄清你想做什么。谢谢!添加了缺少的括号,我有我的applescript,但在重新输入时输入错误。欣赏更干净的版本。顺便说一句,示例代码在设置contactName…
代码行周围不包含任何错误处理,如果选择了取消,则应如此。还有,现在还不清楚你到底想做什么,我重复一遍。。。“这就是说,如果您没有将焦点设置为自动操作之外的文本字段,那么您希望击键命令的输出转到何处?我建议您澄清您试图执行的操作。”我希望短脚本在聚焦输入字段后运行。我在多个地方使用此消息,并且输入字段并不总是在页面上的同一位置。有时在网页上,有时在电子邮件中。我将其设置为宏,以便在需要输入消息的任何位置运行它。工作流报告消息已完成,但消息从未被击键。我不明白为什么它没有按预期的那样运行“设置contactName”部分。