Question

How do you bundle an applescript that has a main script which is reading scripts randomly from a folder?

Était-ce utile?

La solution

Check out this code: http://macosxautomation.com/applescript/linktrigger/index.html.

It's doing exactly that (besides other things) and you can use the sub-routine there for the script-handling.

Here is a full script that works when you follow these steps: save it as an application and put all your scripts into the "Resources" folder of that application (as described at the webpage, "Show Package Contents"). Good Luck.

property MyUserName : ""

if MyUserName is "" then

    display dialog "User Name:" default answer "" buttons {"Cancel", "Continue…"} default button 2
    copy the result as list to {returnedButton, returnedText}

    if returnedButton is "Cancel" then return

    -- What to do when the user did not input any text?
    if returnedText is "" then return -- we stop. 

    set MyUserName to returnedText
    say "Hello," & MyUserName & "!" using "Karen"

else

    display dialog "User name: " & MyUserName buttons {"Ok"} default button 1 with icon 1 giving up after 10

end if

say "For your information, please start the Amuse App everytime you log on... and I will speak to you at random times during your visit with me." using "Karen"

delay 20

try

    repeat 105 times

        set rnd to (random number from 1 to 105)
        set rndFileName to (rnd as text) & ".scpt"
        if my run_scriptfile(rndFileName) is false then -- if the script execution fails,…
            error number -128 -- … stop this app
        end if

    end repeat

on error the error_message number the error_number

    display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
    return

end try



on run_scriptfile(this_scriptfile)
    -- This handler will execute a script file 
    -- located in the Resources folder of this applet
    try
        set the script_file to path to resource this_scriptfile
        return (run script script_file)
    on error
        return false
    end try
end run_scriptfile

This is how it looks with (only) 3 of the scripts you wanna randomly execute:

Autres conseils

Your question is clear but here's an script that loads randomly a script from a folder, if the folder contains any.

set theFolder to choose folder as string
set theFiles to do shell script "ls " & quoted form of posix path of theFolder & " | grep '.scpt$' || true"
if theFiles = "" then return
set theFile to some item of (paragraphs of theFiles)
set randomScript to load script file (theFolder & theFile)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top