从列表中选择后的对话框结果

Dialog results after choose from lists

我为学校编写了一个脚本,可以打开所选科目的文件夹和电子书。您键入要访问的主题,然后它会为您打开该主题的文件夹(在应用程序的资源文件夹中)。但是,有些主题有电子书,所以如果主题有电子书,我会在脚本中显示 choose from list。这很好用,我可以从列表中选择两个结果,这样就可以了。当我开始学习没有电子书的科目时(所以他们不需要 choose from list)它不起作用。比如说,Drama 没有电子书,所以如果我输入 "drama",它会立即打开文件夹。这是我目前所拥有的;谁能告诉我为什么它不起作用?:

set subjectsWithEbook to {"History", "Science", "Maths", "French"}

display dialog "What subject would you like to access?" default answer ""
set theSubject to text returned of the result

if theSubject is in subjectsWithEbook then
choose from list {"Open folder", "Open eBook"} with prompt "What to open?"
set theResult to item 1 of the result
if theResult is "Open eBook" and theSubject is "history" then
    tell application "Safari"
        activate
        open location ""
    end tell
else
    if theResult is "Open folder" and theSubject is "history" then
        tell application "Finder" to open folder ((path to me as string) & "Contents:Resources:Subjects:History")
    else
        if theResult is "Open eBook" and theSubject is "science" then
            tell application "Safari"
                activate
                open location ""
            end tell
        else
            if theResult is "Open folder" and theSubject is "science" then
                tell application "Finder" to open folder ((path to me as string) & "Contents:Resources:Subjects:Science")
            else
                if theResult is "Open eBook" and theSubject is "french" then
                    tell application "Safari"
                        activate
                        open location ""
                    end tell
                else
                    if theResult is "Open folder" and theSubject is "french" then
                        tell application "Finder" to open folder ((path to me as string) & "Contents:Resources:Subjects:French")
                    else
                        if theResult is "Open eBook" and theSubject is "maths" then
                            tell application "Preview" to open (path to me as string) & "Contents:Resources:maths_ebook.pdf"
                        else
                            if theResult is "Open folder" and theSubject is "maths" then
                                tell application "Finder" to open folder ((path to me as string) & "Contents:Resources:Subjects:Maths")
                            else
                                if theSubject contains "Drama" then
                                    tell application "Finder" to open folder ((path to me as string) & "Contents:Resources:Subjects:Drama")
                                end if
                            end if
                        end if
                    end if
                end if
            end if
        end if
    end if
end if
end if

为了适应您的所有情况,甚至更多,我更新了我的第一个脚本,使用之前解释的记录数据结构。

您现在可以定义任意数量的主题,并为每个主题定义要打开的文件夹(如果有)或电子书。对于电子书,您还可以定义用于打开的应用程序:例如,对于 PDF 文件,它可以是 "Preview",如果电子书记录包含 URL,则可以是 Safari。

在我的示例中,我为 Drama 添加了一个必须通过 TextEdit 打开的 txt 文件。

对于数学,这是一个可以通过预览打开的 pdf

对于历史记录,这是一个 URL (google !) 可以通过 Safari 打开。

这只是为了向您展示您可以使用这个新脚本做任何事情。 (我加了很多评论)

(* each record contains 4 values : there are as many records as you need
  Sub : the name of the subject
  EB  : the URL or filename of the ebook. if empty no ebook
  Fol : the name of the folder from Contents/Resources to be open. if empty,no folder to open
  EBApp : the application to be used to open EB. 
          In this exemple Maths uses "Preview", Drama uses texEdit and all others are using Safari
*)
global myRules -- to be use also in sub-routines

-- fill here all your definitions URLs and documents
set myRules to {{Sub:"Maths", EB:"maths_ebook.pdf", Fol:"Maths", EBApp:"Preview"}, {Sub:"History", EB:"www.google.com", Fol:"History", EBApp:"Safari"}}
set myRules to myRules & {{Sub:"Science", EB:"myURL", Fol:"Maths", EBApp:"Safari"}, {Sub:"French", EB:"www.apple.com", Fol:"French", EBApp:"Safari"}}
set myRules to myRules & {{Sub:"Drama", EB:"Drama.txt", Fol:"", EBApp:"TextEdit"}}


set R to display dialog "What subject would you like to access?" default answer "" buttons {"Open folder", "Open ebook", "Cancel"}
set theSubject to Capital(text returned of R) -- make sure 1 letter capital like in subjectsWithEbook
set theChoice to (button returned of R)
set NumItem to ItemList(theSubject)

if NumItem is 0 then -- don't know what to do with this subject not in the list !
display alert "Sorry; Subject " & theSubject & " is not available"
return
end if

set mySubject to item NumItem of myRules
if (theChoice is "Open folder") and (Fol of mySubject is not "") then
tell application "Finder" to open folder ((path to me as string) & "Contents:Resources:Subjects:" & (Fol of mySubject))
end if

if (theChoice is "Open ebook") and (EB of mySubject is "") then
display alert "Sorry; Subject " & theSubject & " has no ebook version available"
return
end if

if (theChoice is "Open ebook") and (EB of mySubject is not "") and (EBApp of mySubject is not "") then
if EBApp of mySubject is "Safari" then
    tell application "Safari" to open location (EB of mySubject)
else
    tell application (EBApp of mySubject) to open (EB of mySubject)
end if
end if

on ItemList(LSubject) -- return the item number of the subject in myRules
set LItem to 0
repeat with I from 1 to (count of myRules)
    if LSubject = Sub of item I of myRules then set LItem to I
end repeat
return LItem
end ItemList

on Capital(Localtext) -- set 1st letter in capital and other small caps.
set Letters to every character of Localtext
repeat with I from 1 to count of Letters
    set A to ASCII number of (item I of Letters)
    if (I = 1) and (A > 96) and (A < 123) then set A to A - 32
    if (I > 1) and (A > 64) and (A < 91) then set A to A + 32
    set item I of Letters to ASCII character of A
end repeat
return Letters as string
end Capital