Try 语句在 AppleScript 中起作用

Try statements acting up in 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 }'"
    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

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

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

您在脚本末尾放置了一个 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 }'"
    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

没关系,我找到了一个有效的方法! :)

tryon error 之间,我添加了 set quitFunction to "0" .然后在 on errorend try 之间,我添加了 set quitFunction to 1。在 end try 之后,我让脚本测试变量 quitFunction 是否为 1。如果是,它会退出,如果不是,它不会退出。