Programming AppleScript Editor, Code - applescript

So far i have this but i want to make it to where the finder window will open as many times and the variable "amount" is set to
set volume 10
set amount to text returned of (display dialog "How many times?" default answer "More than 10")
tell application "Finder" to make new Finder window
repeat amount times
end repeat
If someone could help me would be much appreciated.
(Script is not done yet)

Fast answer:
Move the part you want to be repeated inside the repeat .. end repeat block:
set amount to text returned of (display dialog "How many times?" default answer "More than 10")
repeat amount times
tell application "Finder" to make new Finder window
end repeat
Maybe you should check if the user really entered a numeric value and if it is greater than 10 in your next steps.
Enjoy, Michael / Hamburg

Related

Repeat in an if block?

I am trying to make a program that requires the person to select a file and am trying to confirm it. I ran into an issue when I tried to add a repeat in the if block. Is there any workaround for this because this is incredibly frustrating. Thanks in advance! :)
display dialog "Select the program"
tell application "Finder"
set filePath to POSIX path of (choose file)
end tell
display dialog "Are you sure this is the program that you have selected?"
buttons {"Yes", "No"}
if the button returned is "No" then
end repeat
else
Even though this may lead to my death, I'll answer. Study this:
set correctChoice to false
repeat until correctChoice is true -- "is true" is actually unnecessary
--I took this next line out because it's unnecessary - you can put this text in the prompt of the choose file, below
-- display dialog "Select the program"
-- this doesn't need to be (and shouldn't be) in a Finder tell block, so I took that out, too:
set filePath to POSIX path of (choose file with prompt "Select the program")
set myQuery to display dialog "Are you sure this is the program that you have selected?" buttons {"Yes", "No"}
if the button returned of myQuery is "No" then
--there is no repeat loop! Where do you want it? I assume you want the repeat outside of this process
--end repeat
else
set correctChoice to true
end if
end repeat
--maybe do other stuff
Doing what I assume you were attempting means putting the whole thing inside a repeat loop, which stops when a boolean variable is set to true. The if/then either keeps the original false value of the boolean, or sets it to true, thus allowing us to leave the repeat loop.
A "workaround" is a term for something needed to work within limitation or bug in language. You didn't need a workaround - you need to get your code right. Start simple (!!) and learn how various blocks work before you try to force the code to do what you think it should do.

Applescript quicklook: arrow key down 1000 times

I'm a total beginner in apple script.
I would like to open a folder directory that contains hundreds of photos and to view each photo 1 second with quick look and then go to the next (suppose to use down arrow / key code '124').
I don't have any idea of how to build the script. I tried to compile some formulas from other questions or use the manual rec but it doesn't work.
Thanks!
Try following script. I made the delay longer so you have time to stop the script (to test it):
set timePerPreview to 5
set thisFolder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "Finder"
activate
open folder thisFolder
select every item in folder thisFolder
delay 2
set fileCount to (count items in (get selection)) # (count files in folder thisFolder) is faster but counts also .DS_Store and other invisible files
if fileCount = 0 then
beep
return
end if
end tell
pressSpaceInFinder()
delay timePerPreview / 2 # first it has to open the window which seems to need a little time
repeat fileCount - 1 times # -1 because the first item is already displayed
delay timePerPreview
# do shell script "sleep" & space & timePerPreview as text
tell application "System Events"
tell application process "Finder"
set frontmost to true
# cursor right = 124, left = 123
key code 124
end tell
end tell
end repeat
delay timePerPreview
pressSpaceInFinder()
on pressSpaceInFinder()
tell application "System Events"
tell application process "Finder"
set frontmost to true
keystroke space
end tell
end tell
end pressSpaceInFinder
Be aware that it is hard to stop the script since the Finder gets activated every second. Will see if I can add a check to see if the Preview window is open and if not, stop the script.
Also: (without that check) the script will, when the Preview window is open before running, close it but you can just press the space key to open it again.
Also, the first part (getting the file count) isn't so great and may fail when the delay is to short. We could scan the whole folder for images and only count / select those (it has a file template for the scanner, see menu File) but of course you can remove the whole counting thing and just do repeat 1000 times.

AppleScript to get the active list in Reminders?

Can anyone help me to get the active list on display in the Reminders app on OS X?
According to the applescript dictionary for reminders, the application has a "default list" property which is "the list currently active in the Reminders application."
However, this property always seems to return the first list in order in the lists sidebar, not the list which is actually being displayed and is active. I have found that if I rearrange the order of the lists in the sidebar, I will always get whichever I have made the first list, regardless of which is actually being viewed and worked with.
My application is to create a Keyboard Maestro trigger to run an AppleScript to print the list I am currently working on, but it does not appear that the Reminders app functions as is documented in its dictionary. (I have temporarily used a workaround of having the script pop up a chooser listing all the lists so I can select the one i want to print, but that's inefficient and inelegant).
Thanks!
Yes, you can, but you will have to use the bad GUI scripting. And in a bad way. Look:
--Do some GUI scripting to get the decription of a specific group
tell application "Reminders" to activate
tell application "System Events"
tell process "Reminders"
tell window "Reminders"
tell splitter group 1
tell group 1
set des to get description
end tell
end tell
end tell
end tell
end tell
--This description is in the format "Viewing MyList, 1 reminder" so get the part to the "," from des.
set text item delimiters to ","
set texitems to text items of des
set firstPart to get text item 1 of texitems
--Setting the delimiters back
set text item delimiters to ""
--Jump to charcter 9 of firstPart then converting to text
set listname to characters 9 thru end of firstPart as text
--Now we know the name of the current list, so do whatever you want:
tell application "Reminders" to get list listname
This works. But only if Reminders is open. And if Apple changes Reminders structure...

AppleScript - AppleEvent handler failed

I've got a script than on login, ask a user if they want to watch a movie. For the most part it works great. However on occasion and for reasons unknown to me I get an AppleEvent handler failed error. I've read other post on this error but they all seem to be unique. So, if possible can someone please take a look at my script and tell me why this occasionally pops up and if there's anything i can do to prevent it?
One thing that might help to know, is the one thing in the script that fails when this error occurs is the movie doesn't play. It opens in quicktime but doesn't start.
Thanks in advance, here's the script.
tell application "Welcome" to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question
if answer is equal to "Yes, please" then tell application "QuickTime Player"
set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"
set openMovie to open theMovie
present openMovie
play openMovie
delay 30
quit
end tell
if answer is equal to "No, I've seen it" then tell application "Welcome"
quit
tell application "System Events"
delete login item "Welcome"
end tell
end tell
My guess is that you probably need a delay between opening and playing the movie. Sometimes the code runs faster than the computer can react. If that's the case then the movie may still be trying to open when the code tells the movie to play... thus the error. As such I added 2 repeat loops which checks for things to make sure they're available before proceeding to the next step in the code. You also need "open file" in the code instead of just "open".
Your approach in your if statements of telling an application to do something is unusual. I wouldn't do that. I would also combine your if statements into one if/else if statement. Anyway, here's how I would write your code (I'm assuming application "Welcome" is the code itself). I hope this helps!
set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"
tell me to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question
if answer is equal to "Yes, please" then
tell application "QuickTime Player"
activate
set openMovie to open file theMovie
-- delay until the movie opens
set startTime to current date
repeat until exists document 1
delay 0.2
if (current date) - startTime is greater than 10 then return -- a precaution so you don't get stuck in the repeat loop forever
end repeat
present openMovie
play openMovie
-- delay until the movie stops playing
repeat until document 1 is not playing
delay 1
end repeat
quit
end tell
else if answer is equal to "No, I've seen it" then
tell application "System Events" to delete login item "Welcome"
end if

Select a particular tab in terminal depending upon the content using applescript and best practices

This is an application specific problem. I am trying to find and select a tab in Terminal.app depending on contents within. Here is what I'm doing:
tell application "Terminal"
set foundTabs to (every tab of every window) whose contents contains "selectme"
repeat with possibleTab in foundTabs
try
set selected of possibleTab to true
end try
end repeat
end tell
This isn't acting as expected and is pretty foolproof. I wonder if someone can suggest a way to do this with much less code (for instance, the looping shouldn't really be necessary, but applescript is an elusive language).
Thanks
Thing is, the following Applescript will do what you want, but unless your "selectme" string is very unique, you will find it in many tabs.
But anyway, here you go:
tell application "Terminal"
set allWindows to number of windows
repeat with i from 1 to allWindows
set allTabs to number of tabs of window i
repeat with j from 1 to allTabs
if contents of tab j of window i contains "selectme" then
set frontmost of window i to true
set selected of tab j of window i to true
end if
end repeat
end repeat
end tell

Resources