I’m building an Applescript that launch FileZilla, then import a queue from an xml file. The problem is, when a download was previously started and the file already exists on the disk, FileZilla pops a window named “Target file already exists” giving you the choice of overwriting, resuming, replacing, etc the file. So I’m looking for a way to check if this window “exists” (I don’t know if its the right term) and if it does, press tab four times to select resume and then press return.
Here’s what I have so far. The “repeat until exists window “Target file already exists”” part does not seem to be working. BTW, the indentation is all messed up when copying the code in here. Sorry for that…
activate application "FileZilla"
tell application "System Events" to tell process "FileZilla"
click menu item "Import..." of menu 1 of menu bar item "File" of menu bar 1
keystroke "h" using {command down, shift down}
delay 1
keystroke "documents"
key code 124 # right arrow
keystroke "queue"
keystroke return
keystroke return
keystroke return
keystroke "p" using command down
delay 3
repeat until exists window "Target file already exists"
beep 2
delay 3
repeat 4 times
key code 48 # press tab
delay 1
end repeat
key code 36 # press return
end repeat
end tell
tell application "System Events" to tell process "FileZilla"
set frontmost to true # ADDED
click menu item "Import..." of menu 1 of menu bar item "File" of menu bar 1
(2) Am I guessing wrongly ?
I assume that the group of instructions :
repeat 4 times
key code 48 # press tab
delay 1
end repeat
key code 36 # press return
is supposed to be executed when the popup window is displayed.
If I am right, it must be moved out of the waiting loop :
activate application "FileZilla"
tell application "System Events" to tell process "FileZilla"
set frontmost to true
click menu item "Import..." of menu 1 of menu bar item "File" of menu bar 1
keystroke "h" using {command down, shift down}
delay 1
keystroke "documents"
key code 124 # right arrow
keystroke "queue"
keystroke return
keystroke return
keystroke return
keystroke "p" using command down
delay 3
# Wait for the popup window
repeat until exists window "Target file already exists"
beep 2
delay 3
end repeat
# Now the popup is displayed, act in it
repeat 4 times
key code 48 # press tab
delay 1
end repeat
key code 36 # press return
end tell
I just have a question : what is the script supposed to do if there is no need for the app to display the popup ?
The script import a queue.xml file (a queue exported from FileZilla that is imported back into FileZilla). Let’s say I start a download at 5pm and at 6pm the download is not yet finished but I need to turn off my computer. I have another script that export the current queue before shutting down the computer. Now, when the computer boots, I want the “import” script to run so it imports the same queue and resume the download. Since the download has already been started, FileZilla pops up a window that asks me if I want to overwrite the file or resume it. But the same windows won’t pop up if it’s a new download. I want this chunk :
repeat 4 times
key code 48 # press tab
delay 1
end repeat
key code 36 # press return
to run ONLY if this specific window is brought up. If it’s a new download, the window won’t pop up, therefore skip this chunk and do the rest. Does it makes sense?
Right now, with the current code, the script keeps waiting for the window “Target file already exists” to appear. It doesn’t skip it.
activate application "FileZilla"
tell application "System Events" to tell process "FileZilla"
click menu item "Import..." of menu 1 of menu bar item "File" of menu bar 1
keystroke "h" using {command down, shift down}
delay 1
keystroke "documents"
key code 124 # right arrow
keystroke "queue"
keystroke return
keystroke return
keystroke return
keystroke "p" using command down
delay 3
# Wait for the popup window
repeat until exists window "Target file already exists"
end repeat
# Now the popup is displayed, act in it
repeat 4 times
key code 48 # press tab
delay 0.2
end repeat
key code 49 # press space
delay 0.2
repeat 3 times
key code 48 # press tab
delay 0.2
end repeat
key code 49 # press space
key code 36 # press return
end tell
(1) I noticed that since the delivery of Yosemite, when we execute the two instructions :
activate application "FileZilla"
tell application "System Events" to tell process "FileZilla"
the process is not necessarily at front as it is supposed to be.
At first, I introduced a delay between the two instructions but it’s not satisfying because I never know if a delay which is sufficient on my machine will be sufficient on an other one.
Understanding that the problem was just that the process was not at front, it was evident that the correct trick was to put it explicitly at front.
I use : set frontmost to true.
An other coder use : set visible to true.
The result is the same : the process is correctly set to receive the GUI instructions which we send to it.
(2) I was puzzled by your original code because if I read correctly, if the file was not available before the execution of the script, the condition :
exists window “Target file already exists”
will never be satisfied sot the loop will repeat eternally.
And in this case, the instructions written to close the popup window will never be executed.
It’s exactly what you got with your last attempt.
Maybe the correct scheme is to use the opposite condition.
activate application "FileZilla"
tell application "System Events" to tell process "FileZilla"
set frontmost to true
click menu item "Import..." of menu 1 of menu bar item "File" of menu bar 1
keystroke "h" using {command down, shift down}
delay 1
keystroke "documents"
key code 124 # right arrow
keystroke "queue"
keystroke return
keystroke return
keystroke return
keystroke "p" using command down
delay 3
repeat 20 times # If the loop is executed 20 times, it's clear that the file was not previously existing so we will exit the loop.
if exists window "Target file already exists" then
beep 2
delay 3
repeat 4 times
key code 48 # press tab
delay 1
end repeat
key code 36 # press return
exit repeat
end if
delay 3
end repeat
end tell
(3) as I am curious, I wonder why you use : key code 36 # press return
but use 3 times : keystroke return, to achieve the same goal ?
So far it seems to be working. I’ll test it a little bit more tonight.
I’m learning Applescript as I’m building this script. At first I was using keystrokes for every key I needed to type, until I needed to press TAB or F5 for exemple. So I discovered the key codes and thought it would be cleaner to use key codes instead of keystrokes. I need to clean the script a bit.