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.
Related
We have an automation tool for Photoshop, using a control app, which calls Applescripts controlling Photoshop.
One situation is that we must open a RAW image in the CameraRAW plug-in, and then open it in Photoshop. This part is handled, via an applet using System Events. When that applet terminates, we run the processing script for Photoshop.
As some pictures take quite a bit of time to open, we have to make sure that the picture is really open before the script can run. … and that's where I am stuck.
At the moment, I am using the following code which is intended to wait until the image is open (the criterion for "open" is correct (and tested manually), so that's not the issue here).
tell application "Adobe Photoshop CC 2015"
activate
tell application "System Events"
tell application process "Photoshop CC"
--
display alert "Waiting for Window"
--
repeat
try
set wn to name of window 1 as text
try
if (wn contains "(RGB/16") then
set wn to "image is open: " & wn
end if
end try
if (wn contains "(RGB/16") then
display alert "We are there, quitting now… " & wn
exit repeat
end if
end try
delay 1
end repeat
end tell
end tell
--
display alert "Ready for process"
--
-- and here comes the processing code
end tell
I also tried to set a variable which is tested as argument for repeat, and changed when the exit condition is fulfilled.
Trying to create even alerts within the repeat loop, does not lead to any effect; the script ends up in an infinite loop.
It is well possible that I miss the obvious… So, I am grateful for any helpful hint.
Thanks in advance.
I think you have a few small issues with your script that are causing your problem.
You are using name of window 1 when I believe you need name of document 1. With your first try block structured as it was you weren't realizing it was actually giving an error on name of window 1
The name that is returned doesn't contain the color space and bit count, so I've changed the result test to an empty string
Notice the modifications to the try block around getting the document name
I don't believe it's necessary or see a reason to use "System Events" in this case, so I've modified the version below without it.
Example Script
tell application "Adobe Photoshop CC 2015"
display alert "Waiting for Window"
repeat
try
set wn to name of document 1 as text
on error
set wn to ""
end try
try
if wn is not equal to "" then
set wn to "image is open: " & wn
end if
end try
if wn is not equal to "" then
display alert "We are there, quitting now… " & wn
exit repeat
end if
delay 1
end repeat
display alert "Ready for process"
end tell
Playing with AppleScript I have found when you build a repeat to increment through a block if you cancel it you have to close the script to be able to start over:
property foobar : 0
tell application "BBEdit"
activate
select insertion point before first character of text document 1
repeat
## some magic
end repeat
end tell
Is there a way on cancel you can reset the property back to 0 in the script so that it can be rerun again? Also, when the repeat loop is ran and finished you still have to close the script to be able to rerun it, even after setting the foobar back to 0 with set foobar to 0. Research didn't show anything on Apple.se, SO, superuser or Apple discussion. How can you get the script to reset without having to close it?
You can do this, you will be prompted to restart or cancel the script.
property foobar : 0
tell application "BBEdit"
activate
repeat
set foobar to 0
select insertion point before first character of text document 1
repeat
## some magic
end repeat
display dialog "Restart Script" buttons {"Cancel", "Restart"} default button 2
end repeat
end tell
I'm confused on what you need the property property foobar : 0 for later in the script. If you just don't make it a property, it resets every time you run the script, so you don't have the problem... but I feel like I've got to be missing something.
I mean, what is that needs to be retained between runs such that you're making foobar an Applescript property, instead of just defining it as a variable?
set foobar to 0
Let me know what I'm missing here...
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
Thanks for taking the time to read my question.
It's pretty simple, but i am a complete noobie to this, so am having some trouble.
Is it possible to have an applescript that will check the mac app store for updates, and if there are, output the number of updates to someplace?
A good example of this is (if you are aware of it) the geeklets that check for unread mail, and then outputs it to the desktop.
EDIT:
I downloaded a geeklet for the unread mail (as referenced above), and using that as a starting point, I tried to write my own script.
set run_bool to 1
tell application "System Events"
set run_bool to count (every process whose name is "App Store")
end tell
if run_bool is 1 then
tell application "App Store"
set update_count to 0
set output_string to ""
repeat with upd in Apps in Updates
if upd's download is not true then
set update_count to update_count + 1
end if
end repeat
if update_count is 0 then
set output_string to "zero"
else if update_count is 1 then
set output_string to "one"
else
set output_string to "two"
end if
end tell
else
set output_string to "not running"
end if
return output_string
now this is not my final code, but simply to check to see if it will work and what the output would be.
On compilation I get an error saying
error "The variable Updates is not defined." number -2753 from "Updates"
as well as
Syntax Error
Expected end of line but found unknown token
Also, when I stopped compilation, this appeared below the last line in my code
tell application "GeekTool Helper"
activate
«event ascrgsdf»
Any help is appreciated.
#foo is pretty right on with his idea. This code only requires one line. In the second line, I used display notification, but you substitute it with you preferred method to pass on the value.
tell application "System Events" to tell (first application process whose ¬
frontmost is true) to set returnValue to title of ((first menu item whose title ¬
begins with "App Store") of menu "Apple" of menu bar 1)
display notification returnValue
Result:
"App Store…, 1 update"
menu bar items are accessible everywhere (e.g. windowed/numeral desktop mode, fullscreen mode, dock hidden/disabled).
Make sure accessibility is enabled for Script Editor, or whichever app you use to invoke the script, to gain access to the UI.
There is just one weird thing: if I had used begins with "App Store..." instead of begins with "App Store", the code would be a dud. I don't know why - it might has to do with escaped characters and the .... Anyone who knows please enlighten me with a comment.
As for your code, I can tell from AppleScript Dictionary that Updates is not a property of App Store.app. Nor is any other categories in the UI. To get to the Dictionary, open Script Editor and press CMD+SHIFT+O
In addition, if you want to use return statement, you need an explicit handler. In other words, you need to wrap the code between on run and end run.
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