You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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.

Mac mini, macOS 11.6

Posted on Feb 22, 2022 12:11 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 23, 2022 8:24 AM

I received an answer for this on stackoverflow (https://stackoverflow.com/questions/71227642/how-to-move-files-from-most-recently-modified-folder-one-level-up-using-applescr/71227793#71227793 if you're interested).

Similar questions

1 reply

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

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