Silas Ellery
Silas Ellery

Reputation: 11

Apple Script: how do I watch for new download file with specific name to trigger a script?

I have a script that downloads a report from an online service, waits a specified amount of time e.g. 30secs for the file to actually download and then renames the file. The script then repeats

What I would like to do is instead of putting a static delay in the script, to create a trigger that looks for the new file and once it appears triggers the renaming portion of the script.

The downloaded file name is constant.

The current portion of the script looks like this

`

tell application "Finder" to activate
tell application "System Events"
        delay Wait_Time
        key code 125
        tell application "Finder"
            set name of (selection as alias) to (the clipboard)
        end tell
    end tell

ultimately what I would like is to replace the delay Wait_Time with a something like:

repeat until file name found
Search Folder for "file name"
end repeat
select new file
rename new file to clipboard

obviously this wont work but it kind of captures my logic.

I have also tried a few other possible solutions but Im new to AppleScript and not overly confident that these approaches are suitable for my application.

The below is an example of a solution I have tried. This is just an example and im not sure if this particular code can even get me to where I need to be. I thought I would include it to show just how lost I am.

set excludes to {"Folder", "Application"}

tell application "Finder"
    
    set search_folder to folder "Macintosh HD:Users:XXXXXXXX:Library:CloudStorage:OneDrive-Flinders:Flinders Connect Stats:Source Stats:Cisco Finesse:Agent State Summary by Interval Report"
    
    set foundItems to (every item in search_folder whose name contains "Agent State Summary by Interval Report" and kind is not in excludes) as alias list
    
    
    if foundItems is {} then return
    
    repeat with once from 1 to 100
        try
            if (count of foundItems) = 1 then
                
                tell application "Finder" to activate
                tell application "System Events"
                    key code 125
                    tell application "Finder"
                        set name of (selection as alias) to (the clipboard)
                    end tell
                end tell
                
                exit repeat
                
            end if
        end try
    end repeat
    return
    
end tell

Im sure you can see I have no idea what im doing.

Anyway any help would be awesome!

Upvotes: 0

Views: 159

Answers (1)

Silas Ellery
Silas Ellery

Reputation: 11

I have found a solution... if anyone has ideas for improvement please let me know.

tell application "Finder" to activate
    tell application "System Events"
        repeat until (exists (files of folder folderPath whose name contains "Agent State Summary by Interval Report"))
            delay 1
        end repeat
    end tell
    
    
    tell application "System Events"
        key code 125
        tell application "Finder"
            set name of (selection as alias) to (the clipboard)
        end tell
    end tell
end repeat
say "Finished!"
display dialog "Completed"

The original solution was found here AppleScript: How to repeat a search for a file until it is found?

Upvotes: 1

Related Questions