在后台显示和更新 applescript 输出
Display and update applescript output in background
我有一个简短而有趣的程序,可以在 applescript 中输出我的内部和外部 ip。
这是 Applescript 代码:
set inIP to IPv4 address of (get system info)
set exIP to (do shell script "curl ipecho.net/plain")
display dialog "Internal: " & inIP & "
External: " & exIP
我希望它在后台不断更新,最好不要像现在这样在显示对话框功能中更新。
我不想 显示对话框 不断弹出,所以我正在寻找,例如,在 菜单栏 中显示 IP。
我不知道这是否可以用 Applescript 来做
从 10.10 开始(我认为)您可以直接在脚本编辑器中使用 ApplescriptOBJC 创建真正的应用程序。
我以前没有真正尝试过,但是一旦开始,它比我预期的要容易。
将此代码粘贴到新的脚本编辑器 Applescript 文档中。
使用 另存为... 菜单选项将其另存为 保持打开应用程序。
然后 运行 该应用程序作为普通应用程序。
使用OP的原始applescript代码
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
end makeStatusBar
-- update statusBar
on displayIP(theDisplay)
StatusItem's setTitle:theDisplay
end displayIP
--repeat run update code
on idle
--get the IPs
set inIP to IPv4 address of (get system info)
set exIP to (do shell script "curl ipecho.net/plain")
set theDisplay to "Internal: " & inIP & " External: " & exIP
my displayIP(theDisplay)
return 30 -- run every 30 seconds
end idle
-- call to create initial NSStatusBar
my makeStatusBar()
该应用设置为每 30 秒 运行。
它将使用您的 ips 更新菜单栏中的状态栏菜单。
我没有进行任何错误检查并留给你。
另请记住,如果您想在脚本编辑器中 运行 代码,请确保使用 "Run Application"。
更新:1
我已经更改了内部 IP 地址代码以使用 NShost,它比“get system info”
更快并且可能更可靠
更新:2
更新外部代码以使用 NSURL 请求而不是原始的 Curl do shell 脚本命令。
如果由于没有网络连接等原因导致无法获取外部 IP 地址,这可以更容易地进行错误检查
Curl 将 return 一个完整的信息日志,说明它失败的原因,恕我直言,这很痛苦。
更新了 applescript 代码
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
end makeStatusBar
-- update statusBar
on displayIP(theDisplay)
StatusItem's setTitle:theDisplay
end displayIP
--repeat run update code
on idle
--get the IPs
set stringAddress to ""
--use NSHost to get the Internal IP address
set inIPAddresses to current application's NSHost's currentHost's addresses
--work through each item to find the IP
repeat with i from 1 to number of items in inIPAddresses
set anAddress to (current application's NSString's stringWithString:(item i of inIPAddresses))
set ipCheck to (anAddress's componentsSeparatedByString:".")
set the Counter to (count of ipCheck)
if (anAddress as string) does not start with "127" then
if Counter is equal to 4 then
set stringAddress to anAddress
-- found a match lets exit the repeat
exit repeat
end if
else
set stringAddress to "Not available"
end if
end repeat
-- Get extenal IP
set anError to missing value
set iPURL to (current application's NSURL's URLWithString:"http://ipecho.net/plain")
set NSUTF8StringEncoding to 4
set exIP to (current application's NSString's stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding |error|:anError) as string
if exIP contains missing value then
set exIP to "Not available"
end if
set theDisplay to "Intl: " & stringAddress & " Extnl: " & exIP
--call to update statusBar
my displayIP(theDisplay)
return 30 -- run every 30 seconds
end idle
-- call to create initial NSStatusBar
my makeStatusBar()
更新 3
这个将按照 OP 在评论中的要求进行操作。
它现在有一个下拉菜单,其中包含外部或内部两个选项。
Select 一个或另一个菜单项将更改状态栏以显示所选 IP。
最后一张拼得很快,所以不漂亮。 :-)
( 更新 4 它还会在退出应用程序并重新启动时保留选择。)
新代码:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
property selectedMenu : "" -- each menu action will set this to a number, this will determin which IP is shown
property theDisplay : ""
property defaults : class "NSUserDefaults"
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
set internalMenuItem to current application's NSMenuItem's alloc()'s initWithTitle:"Internal" action:"showInternal:" keyEquivalent:""
set externalMenuItem to current application's NSMenuItem's alloc()'s initWithTitle:"External" action:"showIExternal:" keyEquivalent:""
StatusItem's setMenu:newMenu
newMenu's addItem:internalMenuItem
newMenu's addItem:externalMenuItem
internalMenuItem's setTarget:me
externalMenuItem's setTarget:me
end makeStatusBar
--Show Internal ip Action
on showInternal:sender
defaults's setObject:"1" forKey:"selectedMenu"
my runTheCode()
end showInternal:
--Show External ip Action
on showIExternal:sender
defaults's setObject:"2" forKey:"selectedMenu"
my runTheCode()
end showIExternal:
-- update statusBar
on displayIP(theDisplay)
StatusItem's setTitle:theDisplay
end displayIP
on runTheCode()
set stringAddress to ""
--use NSHost to get the Internal IP address
set inIPAddresses to current application's NSHost's currentHost's addresses
--work through each item to find the IP
repeat with i from 1 to number of items in inIPAddresses
set anAddress to (current application's NSString's stringWithString:(item i of inIPAddresses))
set ipCheck to (anAddress's componentsSeparatedByString:".")
set the Counter to (count of ipCheck)
if (anAddress as string) does not start with "127" then
if Counter is equal to 4 then
set stringAddress to anAddress
-- found a match lets exit the repeat
exit repeat
end if
else
set stringAddress to "Not available"
end if
end repeat
-- Get extenal IP
set anError to missing value
set iPURL to (current application's NSURL's URLWithString:"http://ipecho.net/plain")
set NSUTF8StringEncoding to 4
set exIP to (current application's NSString's stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding |error|:anError) as string
if exIP contains missing value then
set exIP to "Not available"
end if
set selectedMenu to (defaults's stringForKey:"selectedMenu") as string
if selectedMenu is "" or selectedMenu contains missing value then
set selectedMenu to "1"
end if
if selectedMenu is "1" then
set theDisplay to "Intl: " & stringAddress
else if selectedMenu is "2" then
set theDisplay to " Extnl: " & exIP
end if
--call to update statusBar
my displayIP(theDisplay)
end runTheCode
--repeat run update code
on idle
my runTheCode()
--my displayIP(theDisplay)
return 30 -- run every 30 seconds
end idle
-- call to create initial NSStatusBar
set defaults to current application's NSUserDefaults's standardUserDefaults
my makeStatusBar()
我有一个简短而有趣的程序,可以在 applescript 中输出我的内部和外部 ip。
这是 Applescript 代码:
set inIP to IPv4 address of (get system info)
set exIP to (do shell script "curl ipecho.net/plain")
display dialog "Internal: " & inIP & "
External: " & exIP
我希望它在后台不断更新,最好不要像现在这样在显示对话框功能中更新。
我不想 显示对话框 不断弹出,所以我正在寻找,例如,在 菜单栏 中显示 IP。
我不知道这是否可以用 Applescript 来做
从 10.10 开始(我认为)您可以直接在脚本编辑器中使用 ApplescriptOBJC 创建真正的应用程序。
我以前没有真正尝试过,但是一旦开始,它比我预期的要容易。
将此代码粘贴到新的脚本编辑器 Applescript 文档中。
使用 另存为... 菜单选项将其另存为 保持打开应用程序。
然后 运行 该应用程序作为普通应用程序。
使用OP的原始applescript代码
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
end makeStatusBar
-- update statusBar
on displayIP(theDisplay)
StatusItem's setTitle:theDisplay
end displayIP
--repeat run update code
on idle
--get the IPs
set inIP to IPv4 address of (get system info)
set exIP to (do shell script "curl ipecho.net/plain")
set theDisplay to "Internal: " & inIP & " External: " & exIP
my displayIP(theDisplay)
return 30 -- run every 30 seconds
end idle
-- call to create initial NSStatusBar
my makeStatusBar()
该应用设置为每 30 秒 运行。 它将使用您的 ips 更新菜单栏中的状态栏菜单。
我没有进行任何错误检查并留给你。
另请记住,如果您想在脚本编辑器中 运行 代码,请确保使用 "Run Application"。
更新:1 我已经更改了内部 IP 地址代码以使用 NShost,它比“get system info”
更快并且可能更可靠更新:2
更新外部代码以使用 NSURL 请求而不是原始的 Curl do shell 脚本命令。
如果由于没有网络连接等原因导致无法获取外部 IP 地址,这可以更容易地进行错误检查
Curl 将 return 一个完整的信息日志,说明它失败的原因,恕我直言,这很痛苦。
更新了 applescript 代码
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
end makeStatusBar
-- update statusBar
on displayIP(theDisplay)
StatusItem's setTitle:theDisplay
end displayIP
--repeat run update code
on idle
--get the IPs
set stringAddress to ""
--use NSHost to get the Internal IP address
set inIPAddresses to current application's NSHost's currentHost's addresses
--work through each item to find the IP
repeat with i from 1 to number of items in inIPAddresses
set anAddress to (current application's NSString's stringWithString:(item i of inIPAddresses))
set ipCheck to (anAddress's componentsSeparatedByString:".")
set the Counter to (count of ipCheck)
if (anAddress as string) does not start with "127" then
if Counter is equal to 4 then
set stringAddress to anAddress
-- found a match lets exit the repeat
exit repeat
end if
else
set stringAddress to "Not available"
end if
end repeat
-- Get extenal IP
set anError to missing value
set iPURL to (current application's NSURL's URLWithString:"http://ipecho.net/plain")
set NSUTF8StringEncoding to 4
set exIP to (current application's NSString's stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding |error|:anError) as string
if exIP contains missing value then
set exIP to "Not available"
end if
set theDisplay to "Intl: " & stringAddress & " Extnl: " & exIP
--call to update statusBar
my displayIP(theDisplay)
return 30 -- run every 30 seconds
end idle
-- call to create initial NSStatusBar
my makeStatusBar()
更新 3
这个将按照 OP 在评论中的要求进行操作。
它现在有一个下拉菜单,其中包含外部或内部两个选项。
Select 一个或另一个菜单项将更改状态栏以显示所选 IP。
最后一张拼得很快,所以不漂亮。 :-)
( 更新 4 它还会在退出应用程序并重新启动时保留选择。)
新代码:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
property selectedMenu : "" -- each menu action will set this to a number, this will determin which IP is shown
property theDisplay : ""
property defaults : class "NSUserDefaults"
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
set internalMenuItem to current application's NSMenuItem's alloc()'s initWithTitle:"Internal" action:"showInternal:" keyEquivalent:""
set externalMenuItem to current application's NSMenuItem's alloc()'s initWithTitle:"External" action:"showIExternal:" keyEquivalent:""
StatusItem's setMenu:newMenu
newMenu's addItem:internalMenuItem
newMenu's addItem:externalMenuItem
internalMenuItem's setTarget:me
externalMenuItem's setTarget:me
end makeStatusBar
--Show Internal ip Action
on showInternal:sender
defaults's setObject:"1" forKey:"selectedMenu"
my runTheCode()
end showInternal:
--Show External ip Action
on showIExternal:sender
defaults's setObject:"2" forKey:"selectedMenu"
my runTheCode()
end showIExternal:
-- update statusBar
on displayIP(theDisplay)
StatusItem's setTitle:theDisplay
end displayIP
on runTheCode()
set stringAddress to ""
--use NSHost to get the Internal IP address
set inIPAddresses to current application's NSHost's currentHost's addresses
--work through each item to find the IP
repeat with i from 1 to number of items in inIPAddresses
set anAddress to (current application's NSString's stringWithString:(item i of inIPAddresses))
set ipCheck to (anAddress's componentsSeparatedByString:".")
set the Counter to (count of ipCheck)
if (anAddress as string) does not start with "127" then
if Counter is equal to 4 then
set stringAddress to anAddress
-- found a match lets exit the repeat
exit repeat
end if
else
set stringAddress to "Not available"
end if
end repeat
-- Get extenal IP
set anError to missing value
set iPURL to (current application's NSURL's URLWithString:"http://ipecho.net/plain")
set NSUTF8StringEncoding to 4
set exIP to (current application's NSString's stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding |error|:anError) as string
if exIP contains missing value then
set exIP to "Not available"
end if
set selectedMenu to (defaults's stringForKey:"selectedMenu") as string
if selectedMenu is "" or selectedMenu contains missing value then
set selectedMenu to "1"
end if
if selectedMenu is "1" then
set theDisplay to "Intl: " & stringAddress
else if selectedMenu is "2" then
set theDisplay to " Extnl: " & exIP
end if
--call to update statusBar
my displayIP(theDisplay)
end runTheCode
--repeat run update code
on idle
my runTheCode()
--my displayIP(theDisplay)
return 30 -- run every 30 seconds
end idle
-- call to create initial NSStatusBar
set defaults to current application's NSUserDefaults's standardUserDefaults
my makeStatusBar()