1

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!

1

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.

  • 1
    Wow, first of all massive thanks for this incredibly detailed response! It's already been amazingly helpful and really helped to get my head around Automator v AppleScript. I've updated the original post to hopefully give some more insight in to what I'm trying to achieve and where I'm up to. I've had varying degrees of success using your examples. I've been able to create an AppleScript script and attach it to the folder and create a message giving me the filename which is great! Though I can't seem to get it to work in automator or pass it through to batch. Thanks again @user3439894 – jeffolaa Jan 7 '18 at 11:37
0

Managed to resolve my blocker thanks to some amazing suggestions from @user34393894.

Instead of using Automator I was able to get the outcome I needed through an AppleScript script attached as a Folder Action.

For anyone interested the code I'm now using which is getting great results is:

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

        tell application "System Events" -- get item info
            set theKind to kind of thisItem
            set theExtension to name extension of thisItem
        end tell

        if theKind is "Folder" then
            -- do folder stuff
        else if theExtension is "mp3" then

            do shell script "/Users/jeffolaa/ReplaceName.sh " & thisItemsFileName

        end if          
    end try
end repeat
end adding folder items to

If anyone has any feedback or builds on the code that would be greatly appreciated, always looking to improve!

  • There really is no need to use System Events to get item info, as info for is an AppleScript built-in and that was already done and is contained in thisItemsInfo for thisItem. That is, the line set thisItemsInfo to info for thisItem already contains the info, just access it like done in the next line but as appropriate for the value of the property you want to retrieve, e.g.: set theKind to kind in thisItemsInfo – user3439894 Jan 7 '18 at 17:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .