尝试使用AppleScript

时间:2015-05-17 17:14:46

标签: applescript

我正试图找出AppleScript中的try语句。我得到它进行编译,但它没有按照我的意愿去做。

try
do shell script "cat " & passData
on error

display dialog "Create a password to protect this file. After creating the password, you will need to enter it every time in order to access the app." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
set pass1 to text returned of result
display dialog "Confirm the password you entered." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
set pass2 to text returned of result

if pass1 is pass2 then
    set setPass to pass2
    set passSalt to random number from 10000000 to 99999999
    do shell script "echo " & setPass & " | shasum -a 512 | awk '{print $1}'"
    set passHash to result
    set cHash1 to random number from 10 to 25
    set cHash2 to random number from 26 to 50
    set cHash3 to random number from 51 to 75
    set cHash4 to random number from 76 to 99
    set hBlock1 to characters cHash1 through cHash2 of passHash
    set hBlock2 to characters cHash3 through cHash4 of passHash
    set finalHash to hBlock1 & passHash & hBlock2
    do shell script "echo " & passSalt & return & cHash1 & return & cHash2 & return & cHash3 & return & cHash4 & " > " & passData
    do shell script "echo " & finalHash & " > " & appPath & "Contents/Resources/FinalHash.txt"

else
    display dialog "The passwords didn't match. Restart the application and try again." buttons {"OK"} default button 1
    quit
end if
quit
end try

如果在第一个和第二个对话框中输入的文本不匹配,我希望应用程序退出,但它只是继续代码的其余部分。如果在框中输入的文本匹配,我还希望应用程序在完成所有散列mumbo jumbo之后退出,但它也不会这样做。我怎样才能解决这个问题?我怀疑它与 try 语句有关。

编辑:当我说它继续代码的其余部分时,我的意思是当我将其作为应用程序导出时。它在编辑器中正常工作,但在应用程序中它继续到代码的其余部分。

2 个答案:

答案 0 :(得分:0)

您在脚本的末尾添加了end try,但没有关闭的开放try

在下面的代码中,我删除了end try。除此之外,我还添加了tell application "finder"end tell以防止您的脚本编辑器退出。

尝试过,它在这里工作正常。

tell application "Finder"
    display dialog "Create a password to protect this file. After creating the password, you will need to enter it every time in order to access the app." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
    set pass1 to text returned of result
    display dialog "Confirm the password you entered." default answer "" buttons {"Cancel", "OK"} default button 2 with hidden answer
    set pass2 to text returned of result

if pass1 is pass2 then
    set setPass to pass2
    set passSalt to random number from 10000000 to 99999999
    do shell script "echo " & setPass & " | shasum -a 512 | awk '{print $1}'"
    set passHash to result
    set cHash1 to random number from 10 to 25
    set cHash2 to random number from 26 to 50
    set cHash3 to random number from 51 to 75
    set cHash4 to random number from 76 to 99
    set hBlock1 to characters cHash1 through cHash2 of passHash
    set hBlock2 to characters cHash3 through cHash4 of passHash
    set finalHash to hBlock1 & passHash & hBlock2
    do shell script "echo " & passSalt & return & cHash1 & return & cHash2 & return & cHash3 & return & cHash4 & " > " & passData
    do shell script "echo " & finalHash & " > " & appPath & "Contents/Resources/FinalHash.txt"
else
    display dialog "The passwords didn't match. Restart the application and try again." buttons {"OK"} default button 1
    quit
end if
end tell

答案 1 :(得分:0)

没关系,我发现了一种有效的方法! :)

尝试和错误 之间,我将 set quitFunction添加到“0”。然后在 on error end try 之间添加 set quitFunction to 1 。在结束尝试之后,如果变量quitFunction为1,我进行了脚本测试。如果是,则退出,如果不是,则不退出。