Set theFolder to (Macintosh HD:Users:jr:Desktop:Dsource)
Set rnd to (random number from 1 to 3)
Set myScript to load script file (rnd) of theFolder
Run script myScript
This is the script I am attempting to use to open random script files from a folder and execute them. I have written 3 or 4 different types of scripts to try and do this but none seem to work. Can someone point out the error or show me how to do this. Thanks
==> 1. make a folder on your desktop named "Dsource"
==> 2. create a script and name it "1". The script shall contain display dialog "Script 1"
so when you start it it shows the script-number. Save/put it into the Dsource folder and repeat that (but change the number in the display dialog) until you have 3 scripts. Note: often the file name extension is hidden in Finder but such scripts have the extension ".scpt".
If above things are given this should work:
run script (load script file (((path to desktop) as text) & "Dsource:" & (((random number from 1 to 3) as text) & ".scpt")))
ADDITION: Longer version which does the same but does it step by step:
try
set pathToMyFolderOnDesktop to (path to desktop as text) & "Dsource:" as alias
set rnd to (random number from 1 to 3)
set rndFileName to (rnd as text) & ".scpt"
set FullPath to pathToMyFolderOnDesktop & rndFileName as text
set myScript to load script (FullPath as alias)
run script myScript
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
This also displays an error if - for example - the folder isn't there (-43).
You are trying to access files using a string. "Macintosh HD:Users:jr:Desktop:Dsource"
is not a folder until you make it one. So, this line
Set myScript to load script file (rnd) of theFolder
is getting the rnd
character from the string (ie M
if rnd
is 1) and trying to do load script
on it.
To make a string into something that Applescript recognizes as a folder, you can add alias
to the front so it's alias "Macintosh HD:Users:jr:Desktop:Dsource"
Assuming, you could get all the files of a folder with the following line, your logic is still a little off.
Set myScript to load script file (rnd) of theFolder
This line would be targeting a file named rnd
. Not the rnd
file of the folder. In other words, this line evaluates to
Set myScript to load script file "Macintosh HD:Users:jr:Desktop:Dsource:1"
Applescript provides the some
command, which allows you to choose a random item from a list
some item of {1, 2, 3}
--> 2
some item of {1, 2, 3}
--> 1
So, gather your existing script files in a list, and choose a random one from that list.
tell application "System Events"
set scriptFiles to files of alias "Macintosh HD:Users:jr:Desktop:Dsource:" whose kind is "Compiled OSA Script"
end tell
if (count scriptFiles) > 0
set myScript to load script (some item of scriptFiles)
run myScript
else
display dialog "No script files were found."
end if
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.