ACB
ACB

Reputation: 141

How do I use OSX's Folder Actions to execute a command in Terminal when a file is added to a folder?

How do I use OSX's Folder Actions to execute a command in Terminal when a file is added to a folder? My understanding is that Folder Actions can trigger AppleScripts when files are placed in them. I tried to modify a sample "added to folder" script, but it's not working.

This is the exact Terminal command I'd like to run on any zip files placed in the folder (and I know this syntax is correct):

for file in *.zip; do zip -d "$file" "__MACOSX*"; done; for file in *.zip; do zip -d "$file" "*.DS_Store"; done; unzip -l \*.zip 

I tried this code in Script Editor (ver. 2.10), but it's not correct syntax:

on adding folder items to theAttachedFolder after receiving theNewItems
    tell application Terminal
        for file in *.zip; do zip -d "$file" "__MACOSX*"; done; for file in *.zip; do zip -d "$file" "*.DS_Store"; done; unzip -l \*.zip 
    end tell
end adding folder items to

Here is what the script looks like in Script Editor if that helps any:

enter image description here

What is the correct syntax? I'm running OSX 10.13.6.

PS: I've seen Run a shell command when a file is added plus several other similar questions, but I'm too new to Folder Actions and AppleScripts to understand how to make them work in my situation.

Upvotes: 0

Views: 834

Answers (1)

Josh
Josh

Reputation: 23

You need to use the syntax:

set currentTab to do script ("[command]")

For example:

tell application "Terminal"
    set currentTab to do script ("ls")
end tell

Would list the current directory. Make sure you also wrap Terminal in quotes: "Terminal"

Upvotes: 1

Related Questions