Search code examples
serviceapplescriptworkflowautomatorfinder

Mac Automater: from a string, get a file


I'm trying to make a shortcut via an automater service that will move the selected file(s) up a directory. It goes as follows:

  1. Get Selected Finder Items

  2. Get Value of Variable Path

  3. Run Applescript:

    on join(someList, delimiter)
    set prevTIDs to AppleScript's text item delimiters
        set AppleScript's text item delimiters to delimiter
        set output to "" & someList
        set AppleScript's text item delimiters to prevTIDs
        return output
    end join
    to split(someText, delimiter)
        set AppleScript's text item delimiters to delimiter
        set someText to someText's text items
        set AppleScript's text item delimiters to {""}
        return someText
    end split
    on run {input, parameters}
        set pathToMe to POSIX path of (item 1 of input as text)
        set newPath to split(pathToMe, "/")
        set revPath to reverse of newPath
        set restList to rest of revPath
        set restList to rest of restList
        set joinPath to join(reverse of restList, "/")
        set source to POSIX file joinPath
        return source
    end run
    
  4. Set Value of Variable Parent

  5. Move Finder Items To Parent

The Applescript parses the first file path in the Path in order to find the item's grandparent, returning it as a POSIX file string. The problem is that the "Move Finder" action only accepts Files/Folders. How can I select the target parent folder with the resulting string in order to pass it to the "Move Finder" action?

Things I've tried:

  • Using mv in a Run Bash Script: the Run Applescript action doesn't seem to return anything to the Run Bash Script; set to input as arguments, "$@" is always empty.
  • Doing a tell finder in Run Applescript. No error or warning, just nothing happens.
  • Manually setting the value of the parent variable.

Thanks in advance!


Solution

  • return a path of type alias in a list instead of a posix file

    on run {input, parameters}
        set pathToMe to (item 1 of input) as text
        set f to my getParent(pathToMe, ":")
        return {f as alias}
    end run
    
    to getParent(someText, delimiter)
        set prevTIDs to AppleScript's text item delimiters
        set AppleScript's text item delimiters to delimiter
        set n to -2
        if someText ends with ":" then set n to -3
        set t to text 1 thru text item n of someText
        set AppleScript's text item delimiters to prevTIDs
        return t
    end getParent