I am working on the same app that I mentioned in my first question. I have gotten much farther, but when I try to play "2048" the first time, AppleScript give me the error:
"The variable result is not defined"
I will skip the main bulky body of the app and get to the area with the problem:
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
else
display dialog "Invalid response" with title "Invalid response" buttons {"Go back", "Quit"} default button 1
end if
if the button returned of the result is "Go back" then
display dialog "What would you like to do?
Sleep = Go to sleep
Finder = Open Finder
Time = Display current time and date
2048 = play 2048
Quit = Quit application" default answer "" with title "Control panel"
else
return
end if
if the text returned of the result is "Sleep" then
tell application "System Events" to sleep
display dialog "Hello, welcome back!" buttons {"Thank you!"} default button 1
return
else if the text returned of the result is "Finder" then
tell application "Finder" to make new Finder window
else if the text returned of the result is "Time" then
set a to (current date) as list
set AppleScript's text item delimiters to linefeed
set a to a as text
display dialog a buttons {"OK"} default button 1
else if the text returned of the result is "Quit" then
return
else if the text returned of the result is "2048" then
tell application "Terminal"
do script "/Users/student/Documents/2048.sh"
activate
end tell
end if
Just set the result of the display dialog to a variable and use the variable. It's tricky to use "result" as you have in many if statements. One of them is bound to cause a problem because "result" will change at some point.
So do something like this right after your display dialog statement...
set {buttonReturned, textReturned} to {button returned of result, text returned of result}
Then in your if statements use buttonReturned or textReturned.
Related
I have a script that manipulates a GUI. At some point in the process, a progress indicator appears in the GUI for a few mins. If the user clicks the GUI button button "Stop" of sheet 1 (so not a dialog button), I'd like the script to display dialog followed by error number -128. How do I do this? Here's what I tried...
repeat while progress indicator 1 of sheet 1 exists
try
set button_returned to button returned of button "Stop" of sheet 1
if button_returned is "Stop" then
display dialog "Operation cancelled"
error number -128
end if
end try
end repeat
Note: I use repeat while progress indicator 1 of sheet 1 exists to pause the script whilst the progress indicator is up.
Save this example as usual app to see the correct workaround to solve your problem:
global itemCount
set processName to name of current application
set theList to {"Marlow", "Maddie", "Sammy", "Stuey", "Jessie", "Tolstoy", "Marlow", "Maddie", "Sammy", "Stuey"}
set itemCount to count of theList
set progress total steps to count of theList
repeat with i from 1 to itemCount
set thisItem to item i of theList
set progress description to "Item " & i & " of " & itemCount
set progress additional description to thisItem
-- The delay is simply to simulate processing time
-- so you can see the progress bar in action.
-- Exclude this from your code and put your real do stuff.
delay 1
set progress completed steps to i
end repeat
on quit
if progress completed steps < itemCount then
display dialog "Operation cancelled"
end if
end quit
When I execute this script, everything works fine, but then when I press the cancel button, the script just give me a error, "Can’t get item 1 of false"
set mailList to {"Hide Applications", "Quit Applications", "Full Volume"}
set mailType to choose from list mailList
if item 1 of mailType is "Hide Applications" then
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
set the collapsed of windows to true
end tell
else if item 1 of mailType is "Full Volume" then
set volume output volume 100
else if item 1 of mailType is "Quit Applications" then
tell application "System Events" to set the visible of every process to true
set white_list to {"Finder"}
try
tell application "Finder"
set process_list to the name of every process whose visible is true
end tell
repeat with i from 1 to (number of items in process_list)
set this_process to item i of the process_list
if this_process is not in white_list then
tell application this_process
quit
end tell
end if
end repeat
on error
tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try
end if
You add a check if mailType is a boolean which would happen if you press cancel. The check would be inserted between line 2 and 3 and would look something like this:
...
if class of mailType = boolean then return
...
choose from list returns boolean false when the Cancel button is pressed.
Just abort the script immediately in this case
set mailType to choose from list mailList
if mailType is false then return
ƒIt's caused when run through osascript from terminal using the command line "osascript Export\ Library.scpt /Users/bryandunphy/Development/iTunesLibraryConsolidator testing.xml".
same script as my last (solved) question but posting it's entirety this time.
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menuClick(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menuClickRecurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menuClick
on menuClickRecurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menuClickRecurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menuClickRecurse
-- Created by Bryan Dunphy during January of 2017
--
-- select the folder "directory" in Window "WinName" of Application "appName" and then clicks the default button if requested
-- if WinName is "" then it uses the frontmost window (to allow for unnamed windows)
-- REQUIRES "on handleDir" and "on findRoot" to work!
-- ONLY call switchDir
-- "createIt" is a boolean that will create any missing directories if it is set to "true".
-- "selectDefault" is a boolean indicating whether or not to click the window's default button after selecting the specified directory
-- returns "true" or "false" to indicate success.
-- clicks "Cancel" button on failure
-- Always returns "true" if "createIt" is set to "true"
on switchDir(directory, winName, appName, createIt, selectDefault)
local dirs, delim
set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set dirs to every text item of directory
tell application "System Events" to tell process appName to set frontmost to true
my findRoot(appName, winName)
repeat with dir in dirs
if not handleDir(dir, winName, createIt) then
tell application "System Events" to tell process appName to tell button "Cancel" to click
return false
end if
end repeat
if selectDefault then keystroke return
return true
end switchDir
on handleDir(dir, winName, appName, createIt)
local foundIt
foundIt = false
local ndx
if winName is not "" then
repeat with ndx from 1 to (count of window winName's list 1)
if window winName's list 1's item ndx's value is equal to dir then
select window winName's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
else
repeat with ndx from 1 to (count of front window's list 1)
if front window's list 1's item ndx's value is equal to dir then
select front window's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
end if
if not foundIt then
if createIt then
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
else
tell application "System Events" to tell process appName to tell its front window
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
end if
end if
else
return foundIt
end if
end handleDir
on findRoot(appName, winName)
local rootName
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
else
tell application "System Events" to tell process appName's front window
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
end if
return rootName
end findRoot
on run (clp)
if clp's length is not 2 then error "Incorrect Parameters"
local destination, libraryName
set destination to clp's item 1
set libraryName to clp's item 2
menuClick("iTunes", "File", "Library", "Export Library…")
set value of parentObject's text field "Save As:" to (libraryName and ".xml")
tell pop up button 1 of group 1 of window "New iTunes Library" of process iTunes of application "System Events" to click
repeat with ndx from 1 to (count of parentObject's menu 1)
if title of menu item ndx is "" then
select menu item (ndx - 1)
exit repeat
end if
end repeat
my switchDir(destination, "iTunes", "iTunes", true, false)
set the value of text field "Save As:" of window "iTunes" to (libraryName + ".xml")
tell button "Save" of window "iTunes" to click
return (destination and "/" and libraryName and ".xml")
end run
Change:
menuClick("iTunes", "File", "Library", "Export Library…")
to:
menuClick({"iTunes", "File", "Library", "Export Library…"})
AppleScript's error reporting is truly awful (no tracebacks, for starters), and osascript is even worse than Script Editor for debugging. If you run the script in SE, it will at least highlight the line in your script where the error occurred. If that doesn't give you a clue, add log commands to report the script's progress. osascript will write logged messages to stderr. In SE, click the awful 'document' (show/hide log) icon at the bottom of the window, then select 'Messages'. If your time is worth more than $100, get yourself a copy of Script Debugger which also lets you add breakpoints and step through and inspect variables as the script runs.
The Applescript results are Display "end tell"
I don't know why this is happening and need it fixed.
When I click next page for the second time, it "end tell"s randomly
This is the script:
display dialog "What would you like to launch? choose wisely!" buttons
{"Next Page", "cancel", "Google Chrome"} default button "Next Page"
if result = {button returned:"Google Chrome"} then
tell application "Google Chrome" to activate
else if result = {button returned:"cancel"} then
else if result = {button returned:"Next Page"} then
display dialog "Page 2" buttons {"Next page", "Mari0", "Minecraft"} default button "Next page"
if result = {button returned:"Minecraft"} then
tell application "Minecraft" to activate
else if result = {button returned:"Mari0"} then
tell application "Terminal"
activate
do script with command "open /Users/_________/Desktop/Mari0.app/"
delay 1
quit
end tell
else if return = {button returned:"Next page"} then
display dialog "Page 3" buttons {"Safari", "Roblox", "Next Page"} default button "Next Page"
if result = {button returned:"Roblox"} then
tell application "Google Chrome" to open location "http://www.roblox.com/home"
else if result = {button returned:"Safari"} then
tell application "Safari" to activate
else if return = {button returned:"Next Page"} then
display dialog "Final Page" buttons {"iMessages", "Applescript Folder", "Back to Start"} default button "Back to start"
if return = {button returned:"Back to start"} then
run script (open applications)
else if return = {button returned:"iMessages"} then
tell application "Messages" to activate
else if return = {button returned:"Applescript Folder"} then
do shell script "open /Users/__________/Desktop/Applescript/"
end if
end if
end if
end if
These are the results from the script:
tell application "Script Editor"
display dialog "What would you like to launch? choose wisely!"
buttons {"Next Page", "cancel", "Google Chrome"} default button "Next
Page"
--> {button returned:"Next Page"}
display dialog "Page 2"
buttons {"Next page", "Mari0", "Minecraft"} default button "Next page"
--> {button returned:"Next page"}
End tell
Since you are running this test in Script editor you are telling Script Editor to run it. Naturally the tell block ends with end tell. The script has ended and Script Editor is done with its job.
I have been trying to write some applescript that checks to see if the Downloads window is open in Safari and if it is to click and open the last file in the list which is the last file that was downloaded but have been having some issues using Accessibility Inspector I get the following :
<AXApplication: "Safari">
<AXWindow: "Downloads">
<AXScrollArea>
<AXList>
<AXGroup: "ExcelTest.xls">
<AXButton: "file icon">
Attributes:
AXRole: "AXButton"
AXRoleDescription: "button"
AXHelp: "Open"
AXFocused: "false"
AXParent: ""
AXWindow: ""
AXTopLevelUIElement: ""
AXPosition: "x=1062 y=396"
AXSize: "w=32 h=32"
AXDescription: "file icon"`
AXEnabled: "true"
Actions:
AXPress - press
I'm not sure how to access the scroll area and list to get at the button.
This works...
set downloadsIsFrontmost to false
tell application "Safari"
set theWindows to name of windows
if (item 1 of theWindows) is "Downloads" then
activate
set downloadsIsFrontmost to true
end if
end tell
if downloadsIsFrontmost then
tell application "System Events"
tell process "Safari"
set theGroups to groups of list 1 of scroll area 1 of window "Downloads"
set lastGroup to last item of theGroups
repeat 2 times
click button 1 of lastGroup
delay 0.05
end repeat
end tell
end tell
end if