Getting Individual Filenames in Automator

applescriptautomatorbashscript

The idea behind a small project I'm working on is to be able to upload a music file and have After Effects pick it up and render it within a video file template I have.

The workflow would be:

Once I upload a new music file to a folder in my iCloud Drive which would be monitored monitored by my iMac. Each new music file triggers an Adobe script which imports the new track to an Adobe After Effects project which exports it to AME and then quits. The music is then moved to another directory.

I've ran in to an issue with passing in a filename to an Adobe script, so to work around this have made a template Adobe script with a placeholder filename (the file path would always be the same) which can be duplicated, have the placeholder filename replaced (via a batch script, though there could be a simpler way of doing it I'd suspect), executed and then deleted.

As a first pass I've been trying to focus on getting the name of new files to the folder, now with mixed success. When running an AppleScript script (.scpt) file attached to the folder I'm able to get the file name displayed and passed to a batch file. The issue now is that it doesn't seem to work with the Automator equivalent.

Code below:

on adding folder items to thisFolder after receiving theseItems
repeat with i from 1 to count theseItems
    try
        set thisItem to item i in theseItems
        set thisItemsInfo to info for thisItem
        set thisItemsFileName to quoted form of name in thisItemsInfo
        display dialog thisItemsFileName buttons {"Continue"}
        do shell script "/Users/jeffolaa/ReplaceName.sh " & thisItemsFileName
    end try
end repeat
end adding folder items to

The ReplaceName batch is a single command:

#!/bin/bash
sed -i.bu 's/EDITTHIS/'$1'/' /Users/jeffolaa/Library/Mobile\ Documents/com\~apple\~CloudDocs/Documents/Adobe\ Scripts/AddMusicExport.jsx 

So far my Automator workflow looks like so:

enter image description here

All help is very much appreciated!

Best Answer

Note: This answer was written based on the original posting of the question and in particular the first three paragraphs of this answer, sans this note. Since then, the OP has been updated to address what was expressed in the first three paragraphs and they may now seem out of context. However, looking at the edit history of the OP one can see the relevance in context of the original question. They will remain for now until I have the time to make an appropriate edit.


Sorry, but your question is not that well defined and it's not totally clear what actions you added to the Automator Folder Action workflow, sans the obvious Run AppleScript action. Nor is it clear if there are any actions added before or after the Run AppleScript action. Additionally, it's not clear what code is in /Users/jeffolaa/pointer.sh or all of what it's doing. Which leads me to believe that just passing the name of the file to pointer.sh may not be enough. Although without seeing the code in pointer.sh, at this point only you know.

Also, the form of the AppleScript code you've shown in the OP is not the proper form for an Automator Folder Action workflow using a Run AppleScript action. The on adding folder items handler is of the type used in an AppleScript script (.scpt) file that is manually assigned to the target folder using the Folder Action Setup... command from the Services context menu in Finder, not Automator, where you need to use the on input handler in the Run AppleScript action.

You also have no error handling code, which is important in Folder Actions and the type and amount can depend on what you have coded in the pointer.sh. In other words, you might want to filter the items added to the folder by type, making sure it's a file not a folder, or a particular type of file with a given extension, etc. This can be done in Automator before it gets to the Run AppleScript action, or in the code of the Run AppleScript action, or in the code in pointer.sh, or a combination of any or all of them.


With that said, the following example AppleScript code is a minimal coding of what makes sense to be able to pass the quoted fully qualified pathname of the file(s) added to the target folder you set in Automator's Folder Action receives file and folders added to [folder] to pass to pointer.sh, as that's typical of an argument passed to a shell command.

Minimal example AppleScript code for use in a Run AppleScript action in an Automator Folder Action workflow:

on run {input, parameters}
    repeat with i from 1 to count input
        try
            set thisItem to item i in input
            set thisItemsPathname to quoted form of POSIX path of thisItem
            do shell script "/Users/jeffolaa/pointer.sh " & thisItemsPathname
        end try
    end repeat
end run

Minimal example AppleScript code for use in an AppleScript script (.scpt) file manually assigned to the target folder using the Folder Action Setup... command from the Services context menu in Finder:

on adding folder items to thisFolder after receiving theseItems
    repeat with i from 1 to count theseItems
        try
            set thisItem to item i in theseItems
            set thisItemsPathname to quoted form of POSIX path of thisItem
            do shell script "/Users/jeffolaa/pointer.sh " & thisItemsPathname
        end try
    end repeat
end adding folder items to
  • Note: To use an AppleScript script (.scpt) file using the Folder Action Setup... command from the Services context menu in Finder, it first must be added to the ~/Library/Scripts/Folder Action Scripts folder that's within your Home folder.

To test the above code, I created the /Users/jeffolaa/pointer.sh file with the following code in it:

#!/bin/bash

mv "$1" "$HOME/Test/"

Which simply moves the files back to the folder from which they were dragged from to the target folder the Folder Action is assigned to, which is a folder within the Test folder. This let me know it was working properly as coded.



Now if it's just the name of the file you want to pass to pointer.sh, you can use the following:

Minimal example AppleScript code for use in a Run AppleScript action in an Automator Folder Action workflow:

on run {input, parameters}
    repeat with i from 1 to count input
        try
            set thisItem to item i in input
            set thisItemsInfo to info for thisItem
            set thisItemsFileName to quoted form of name in thisItemsInfo
            do shell script "/Users/jeffolaa/pointer.sh " & thisItemsFileName
        end try
    end repeat
end run

Minimal example AppleScript code for use in an AppleScript script (.scpt) file manually assigned to the target folder using the Folder Action Setup... command from the Services context menu in Finder:

on adding folder items to thisFolder after receiving theseItems
    repeat with i from 1 to count theseItems
        try
            set thisItem to item i in theseItems
            set thisItemsInfo to info for thisItem
            set thisItemsFileName to quoted form of name in thisItemsInfo
            do shell script "/Users/jeffolaa/pointer.sh " & thisItemsFileName
        end try
    end repeat
end adding folder items to
  • Note: To use an AppleScript script (.scpt) file using the Folder Action Setup... command from the Services context menu in Finder, it first must be added to the ~/Library/Scripts/Folder Action Scripts folder that's within your Home folder.

To test the above code, I edited the pointer.sh file with the following code in it:

#!/bin/bash

say "$1"

Which simply says the name of the file held in thisItemsFileName, which let me know it was working properly as coded.



Note: The example AppleScript code above is just that, and sans wrapping it in a try statement, does not include any other error handling as may be appropriate/needed/wanted, the onus is upon the user to add any appropriate error handling for any example code presented and or code written by the oneself.

That said, if you'd update your question to fill in the missing pieces as mentioned in the opening paragraphs of my answer, I'll be glad to edit these code examples to reflect what appropriate error handling should be added. There's just no sense adding it until I know the particulars.