Search code examples
applescript

how to move files from most recently modified folder one level up using AppleScript (Big Sur)


I'm not a newbie to (or an expert on) scripting and programming, but after a few hours, I am stumped at how to copy a file (after having googled and tried various things for a few hours) using AppleScript in this situation.

I want to move a file from the most recently modified folder one level up. I'm trying to move the file:

/Users/tsg/my downloads folder/newest folder/pdfs/target1.pdf

to

/Users/tsg/my downloads folder/newest folder/target1.pdf

Where "newest folder" is not a constant name, but will change as I download newer folders.

When I run the code below,

    tell application "Finder"
        
        set downloadFolder to POSIX file "/Users/tsg/my downloads folder" as alias
        set latestModifiedFolder to (downloadFolder & (name of last item of (sort every folder of downloadFolder by modification date)))
        set targetFolder to latestModifiedFolder (* Is this necessary? *)
        set pdfFolder to (targetFolder & ":pdfs:")
        set pdfFile to (pdfFolder & ("target1.pdf"))
        move pdfFile to targetFolder
        
    end tell

I get the error

"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type folder." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to folder

If I add the modifier folder: "move pdfFile to folder targetFolder" for the last line before "end tell", I get

"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to integer"

If I add the modifier file "move file pdfFile to folder targetFolder", I get

"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} to integer

If I remove the "move" line altogether, I get the result:

{alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"}

I would actually like to find the file based on a search term, declared as a property (Property searchWord : {"target"}), rather than hard coded, but I couldn't get that to work either.

Code:

    set pdfFile to first file of pdfFolder whose name contains searchWord

Error:

"Can’t get file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}." number -1728 from file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}

There may be more than one pdf containing the word "target", and I'd actually like to move all files containing the search word up one level, and then rename them pdfs.one.pdf and pdfs.two.pdf.

Thanks in advance for any help.


Solution

  • The errors occur because you try to combine multiple incompatible classes.

    When dealing with the Finder the Finder specifier syntax

    file "foo" of folder "bar" of folder "baz" of startup disk
    

    is a reasonable way to avoid those cannot-do-this errors

    tell application "Finder"
        set downloadFolder to folder "my downloads folder" of home
        set folderName to name of last item of (sort every folder of downloadFolder by modification date)
        set targetFolder to folder folderName of downloadFolder
        set pdfFolder to folder "pdfs" of targetFolder
        set pdfFile to file "target1.pdf" of pdfFolder
        move pdfFile to targetFolder
    end tell
    

    or to move all files containing "target" in the file name

    tell application "Finder"
        set downloadFolder to folder "my downloads folder" of home
        set folderName to name of last item of (sort every folder of downloadFolder by modification date)
        set targetFolder to folder folderName of downloadFolder
        set pdfFolder to folder "pdfs" of targetFolder
        set pdfFiles to files of pdfFolder whose name contains "target"
        move pdfFiles to targetFolder
    end tell