Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scripting 如何让脚本文件通过对话框“保存名称”接受用户名,但从该点开始使用不同的脚本_Scripting_Applescript - Fatal编程技术网

Scripting 如何让脚本文件通过对话框“保存名称”接受用户名,但从该点开始使用不同的脚本

Scripting 如何让脚本文件通过对话框“保存名称”接受用户名,但从该点开始使用不同的脚本,scripting,applescript,Scripting,Applescript,我试图通过Applescript脚本接受用户名:我已经完成了这一点。所以我想我需要把这个名字保存到一个文件中,因为这就是我所需要的,每次我点击这个应用程序并检索这个名字时,我都要激活另一个脚本。我的问题是如何让它在第一次运行后不接受原始脚本。 | 一种方法是使用属性 property MyUserName : "" if MyUserName is "" then display dialog "User Name:" default answer "" buttons {"Canc

我试图通过Applescript脚本接受用户名:我已经完成了这一点。所以我想我需要把这个名字保存到一个文件中,因为这就是我所需要的,每次我点击这个应用程序并检索这个名字时,我都要激活另一个脚本。我的问题是如何让它在第一次运行后不接受原始脚本。

|

一种方法是使用属性

property MyUserName : ""

if MyUserName is "" then

    display dialog "User Name:" default answer "" buttons {"Cancel", "Continue…"} default button 2 with icon 0
    copy the result as list to {button_pressed, text_returned}

    if button_pressed is "Cancel" then
        beep
        return
    end if

    if text_returned is not "" then set MyUserName to text_returned

else

    display dialog "Stored user name: " & MyUserName buttons {"Ok"} default button 1 with icon 1

end if

属性值存储在已编译脚本中。当脚本保存为应用程序时,它也可以工作。在AppleScript编辑器中打开并重新编译它会重置值。

因此,此脚本中的用户名会被保存,但当脚本变成应用程序时,它不会再次要求输入名称?只要属性MyUserName为空,脚本将执行第一部分,如果MyUserName不为空,则执行第二部分,然后执行其他部分。属性或更好的属性:属性的值将被保留,直到您再次编译脚本。您可以在编辑器中测试这一点:只要更改脚本中的某些内容并执行它,它就会再次被编译,MyUserName再次为空。在执行脚本并提供用户名后,我将其存储为应用程序,有趣的是,它仍然保留了该值。