Sunday, December 4, 2022

#1 2022-10-18 01:17:46 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 1091

Folder action with a seperate options file.

Simple example of folder action script to use unix command and seperate option file.

The option file could be parameters for the command to execute. In other words there is no need
to edit the folder action script itself. For this example I have used the command inside the action folder script but could maybe also be a seperate file. My approach is not edit the folder action but instead edit a seperate file.

To test make a folder and edit your options to store <options> file in the same target folder.
Edit the script to set the command you like to use.

Make a folder action for the target folder.

Drag and drop files to the folder.

Applescript:

on adding folder items to thisFolder after receiving addedItems
   try
       if (count of addedItems) is greater than 0 then
           repeat with theItem in addedItems
               try
                   tell application "System Events" to set parentFolder to POSIX path of (container of theItem)
                   set posixPathFile to POSIX path of theItem
                   if posixPathFile ends with ".mkv" then
                       -- do something with the files
                       tell application "Terminal" to do script "/usr/local/bin/other-transcode $(cat " & parentFolder & "/options) " & posixPathFile
                   end if
               end try
           end repeat
       end if
   end try
end adding folder items to

Or we could build propertyList file from a dictionary with command and options to drive
the folder action. And store this files in the target folder for easy update.

To test the script you need to uncomment to write a Dict/propertyList to include your settings.
The parent folder is the folder action target directory.

Applescript:

use framework "Foundation"
use scripting additions

on adding folder items to thisFolder after receiving addedItems
   try
       if (count of addedItems) is greater than 0 then
           repeat with theItem in addedItems
               try
                   -- The main command with options
                   -- set theDict to {command:"/usr/local/bin/other-transcode", options:"--version"}
                   
                   -- Get parent folder of receiving items as (POSIX path)
                   set parentFolder to its parentFolder(theItem)
                   
                   -- Write the dictionary in target folder
                   -- (its writeDict:theDict atPath:(parentFolder & "options.plist"))
                   
                   -- We read the plist file, later we could comment out the write to dict.
                   set optionFile to parentFolder & "/options.plist"
                   set theDict to (its readDictAtPath:optionFile)
                   set {theCommand, theOptions} to {command of theDict, options of theDict}
                   
                   -- Get receiving items as (POSIX path)
                   set posixPathFile to POSIX path of theItem
                   
                   -- Check if receiving items match the target items
                   if posixPathFile ends with ".mkv" then
                       -- Do something with the files
                       tell application "Terminal" to do script theCommand & space & theOptions & space & posixPathFile
                   end if
               end try
           end repeat
       end if
   end try
end adding folder items to

on parentFolder(theItem)
   tell application "System Events" to set parentFolder to POSIX path of (container of theItem)
end parentFolder

on writeDict:theDict atPath:thePath
   set theURL to current application's |NSURL|'s fileURLWithPath:thePath
   set theDict to current application's NSDictionary's alloc()'s initWithDictionary:theDict
   theDict's writeToURL:theURL atomically:true
end writeDict:atPath:

on readDictAtPath:thePath
   set theURL to current application's |NSURL|'s fileURLWithPath:thePath
   set theDict to current application's NSDictionary's dictionaryWithContentsOfURL:theURL |error|:(missing value)
   return theDict as record
end readDictAtPath:

The propertyList file could look like this

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>command</key>
    <string>/usr/local/bin/other-transcode</string>
    <key>options</key>
    <string></string>
</dict>
</plist>

Last edited by Fredrik71 (2022-10-19 12:27:41 pm)


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)