WoodyJean
WoodyJean

Reputation: 1

How to change Finder to parent directory in AppleScript?

I need to create a Mac Automator or AppleScript to do the following: 1) Select a bunch of files 2) Rename the extension of selected files 3) Move the renamed files to the parent folder of the parent folder (2 levels up) I can do 1&2 in Automator but I'm assuming I need a script for #3. Any help would be appreciated!

Upvotes: 0

Views: 255

Answers (3)

CJK
CJK

Reputation: 6102

tell application "Finder"
    set OriginalSin to the insertion location
    set Man to a reference to the parent of OriginalSin
    set Lord to a reference to the parent of Man

    if not (the Lord exists) then return "Dawkins was right."

    set the Shadows to make new folder at OriginalSin ¬
        with properties {name:"___EVIL___"}

    move the selection to the Shadows

    set Worthy to a reference to every file in the Shadows
    set the name extension of the Worthy to "god"


    set Forsaken to the name in the Lord's files
    set Righteous to the Worthy where its name is not in the Forsaken

    move the Righteous to the Lord without replacing
    move the Worthy to OriginalSin
    delete the Shadows
end tell

I wouldn't use "god" as an extension name, it's not recognised by the system as a known file type. In fact, consider why you wish you change a file extension manually, which—besides a plain text file—I cannot think of a situation where this would be necessary. macOS uses file extensions to help determine the type of file, and link them to appropriate applications that will no longer be able to serve as openers for the files that you modify.

Actually, the "e" in ".jpeg" really deserves to die

Upvotes: 1

wch1zpink
wch1zpink

Reputation: 3142

This AppleScript code works for me using the latest version of macOS Mojave.

This following code should do everything that you need.

property setNameExtension : "jpg" -- Change To Whatever You Want

tell application "Finder"
    set selectedFiles to selection as alias list -- Currently Selected Files In Finder
    set containerOfContainerOf to container of (container of item 1 of selectedFiles)
    repeat with i in selectedFiles
        set renamedItems to move i to containerOfContainerOf
        set the name extension of renamedItems to setNameExtension
    end repeat
end tell

Upvotes: 0

Pat_Morita
Pat_Morita

Reputation: 3545

this should get you started

tell application "Finder" to get (container of (path to me)) as text

where path to me could be any HFS path (like the path to one of your files)

Upvotes: 0

Related Questions