AppleScript Return Screen to dialog box

I am writing a script and want to hem in the user(s) in the future from errors. The big one I'm working on right now is that the Choose File command box for Finder or AppleScript (doesn't matter) do not contain the "giving up after" option. So while I can set the timeout to a very large number of seconds (5000 for example), I can't get the box to close and reopen without the Apple Events timing out.


So here is one option I've tried. but the problem I have is that if I swipe to another screen, even if I Activate the finder, it will say that it can't find the window "Choose a File"


Is there a way to get the window to follow the swipe or a command with activate that will bring the finder window to the current screen, even if I'm working in say Safari?


The error occurs when I swipe to another screen; see the error below:

error "System Events got an error: Can’t get window \"Choose a File\" of process \"Finder\"." number -1728 from window "Choose a File" of process "Finder"


Script:


try

with timeout of 5 seconds

tell application "Finder"

set theFilestoChoose to every item of (choose file with prompt "Please select the file(s) you would like to move and rename:" with multiple selections allowed) as list

end tell

end timeout

on error errStr number errorNumber

if errorNumber is -1712 then --timeout error

my closeWindow() --call handler to close window

end if

end try

on closeWindow()

tell application "System Events"

delay 2 -- for observation testing purposes

set frontmost of process "Finder" to true

delay 2 -- for observation testing purposes

click button "Cancel" of window "Choose a File" of process "Finder"

end tell

end closeWindow

MacBook Pro (13-inch Mid 2012), OS X Mavericks (10.9.5)

Posted on Mar 15, 2015 8:56 PM

Reply
4 replies

Mar 16, 2015 6:42 AM in response to tc8213

Hi,


If there is already an window in the Finder, the script swipe to another screen.

Try this :


tell application "Finder"
    if not (exists Finder window 1) then
            set w to make new Finder window
            activate
            close w
    else
            activate
    end if
    set theFilestoChoose to every item of (choose file with prompt "Please select the file(s) you would like to move and rename:" with multiple selections allowed) as list
end tell

Mar 16, 2015 10:11 AM in response to tc8213

If this is to display a dialog, you do not need any application.

So, no need to go to another screen, you can display a dialog in the current screen, like this


try
    with timeout of 5 seconds
        activate
        set theFilestoChoose to choose file with prompt "Please select the file(s) you would like to move and rename:" with multiple selections allowed
    end timeout
on error errStr number errorNumber
    if errorNumber is -1712 then --timeout error
        my closeWindow() --call handler to close window
    end if
end try
on closeWindow()
    tell application "System Events"
        click button "Cancel" of window "Choose a File" of (first process whose frontmost is true)
    end tell
end closeWindow

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript Return Screen to dialog box

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.