在后台显​​示和更新输出的AppleScript [英] Display and update applescript output in background

查看:191
本文介绍了在后台显​​示和更新输出的AppleScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短而亲切的程序输出我的内部和外部IP AppleScript中。

I have a short and sweet program that outputs my internal and external ip in applescript.

这是AppleScript的code:

Here it is the Applescript code:

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

我想它的背景和$ P $在显示对话框,它在目前不断更新pferably不可以功能。

我不希望有一个显示对话框不断雨后春笋般冒出来,所以我期待例如,在菜单栏中显示了IPS

I do not want a display dialog constantly popping up so I am looking for example, displaying the IPs in the menu bar.

我不知道这是可能的AppleScript做

I do not know if this is possible to do with Applescript

推荐答案

由于从10.10(我认为),您可以创建直接在脚本编辑器中使用ApplescriptOBJC实际应用。

As from 10.10 (i Think) you can create real application using ApplescriptOBJC directly in Script Editor.

我还没有真正尝试过,但一旦你走了它比我想象得容易。

I have not really tried it before but once you get going it is easier than I expected.

本贴code。

将其保存为继续开放应用程序使用的另存为... 菜单选项。

Save it as a Stay open Application using the Save as… menu option.

然后运行该应用程序作为一个正常的应用程序。

Then run the app as a normal application.

使用OP原有的AppleScript code

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菜单栏状态栏菜单。

The app is set to run every 30 seconds. It will update a status bar menu in the menu bar with your ips.

我没有把任何错误检查并留给你。

I have not put any error checking in and leave that to you.

还要记住,如果你想运行code,而在脚本编辑器,那么要确保使用运行应用程序。

Also remember if you want to run the code while in Script Editor then make sure you use "Run Application".

更新:1
我已经改变了内部IP地址code使用NShost这是比获取系统信息

更新:2

更新外部code使用NSURL要求而不是原来的卷曲做shell脚本命令。

Update the external code to use a NSURL request rather than the Original Curl do shell script command.

这使得更容易错误检查,如果是在获得外部IP地址失败,由于没有网络连接...等。

This allows for easier error checks if the is a failure in obtaining the external ip address due to no network connection...etc.

卷曲将信息的整个日志返回作为失败的原因,并恕我直言,有疼痛感。

Curl will return a whole log of info as to why it failed and be IMHO a pain.

更新的AppleScript code

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在评论中问道。

This one will do as the OP asked in the comments.

现在有一个下拉菜单中有两个选项外部或内部。

It now has a drop down menu with two options External or Internal.

选择一个或其他菜单项将改变状态栏上显示所选择的IP。

Select one or the other menu item will change the status bar to show the chosen IP.

这最后一条很快被扔在一起,所以它不是pretty。 : - )

This last one was thrown together quickly so it is not pretty. :-)

更新4 它也仍然存在上退出应用程序,并在重新启动这个选择)。

( UPDATE 4 It also persists the selection on quitting the app and relaunching. )

新的code:

  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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆