rakeshsingh93

Q: can i convert the choose option in applescript in Auto folder action setup

hi,

 

I h've an applescript which is work to sorting a folder image and arrange it to folder wise according to its name uniqueness.

 

In this script have choose folder option but i want to convert the option in automatically like Folder action setup. like

on adding folder items to this_folder after receiving these_items

 

Here is my script

 

set d to choose folder

_main({d})

on _main(argv)

    script o

        property ff : {} -- list of aliases

        property gg : {} -- list of aliases to be moved per prefix

        property nn : {} -- list of file names

        property mm : {} -- list of prefixes

        property mm1 : {} -- list of distinct prefixes

       

        repeat with d in argv -- per source directory

            set d to d's contents

            -- retrieve list of aliases and names for document files

            tell application "Finder"

                tell item d's document files to set {ff, nn} to {it as alias list, name}

            end tell

            try

                set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"_"}}

                repeat with i from 1 to count my nn

                    set {n, m} to {my nn's item i, ""}

                    if (count n's text items) > 1 then set m to "" & n's text items 1 thru -2

                    set my mm's end to m

                    if m ≠ "" and m is not in my mm1 then set my mm1's end to m

                end repeat

                set AppleScript's text item delimiters to astid0

            on error errs number errn

                set AppleScript's text item delimiters to astid0

                error errs number errn

            end try

            repeat with m in my mm1 -- per distinct prefix

                set m to m's contents

                -- prepare destination for this prefix

                try

                    set dst to (d as string) & m & ":" as alias

                on error number -43 -- dst not present

                    tell application "Finder" to set dst to (make new folder at d with properties {name:m}) as alias

                end try

                repeat with i from 1 to count my mm

                    if my mm's item i = m then set my gg's end to my ff's item i

                end repeat

                if gg ≠ {} then tell application "Finder" to move gg to dst with replacing

                set gg to {}

            end repeat

        end repeat

    end script

    tell o to run

end _main

 

so if support to convert this script to folder action script so please support to do it.

 

Thanks

Rakesh

Mac Pro, Mac OS X (10.5.5)

Posted on Jun 8, 2016 5:41 AM

Close

Q: can i convert the choose option in applescript in Auto folder action setup

  • All replies
  • Helpful answers

  • by red_menace,

    red_menace red_menace Jun 8, 2016 11:29 AM in response to rakeshsingh93
    Level 6 (15,541 points)
    Desktops
    Jun 8, 2016 11:29 AM in response to rakeshsingh93

    You just need to provide multiple ways to call your main handler, for example:

     

    on run -- application double-clicked or run from the Script Editor
      set d to choose folder
      _main({d})
    end run
    
    on open droppedItems -- items dropped onto droplet
      _main(droppedItems)
    end open
    
    on adding folder items to this_folder after receiving these_items -- folder action
      _main(these_items)
    end adding folder items to
    
    on _main(argv) -- main routine
      -- whatever
    end _main
    
  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 Jun 9, 2016 2:41 AM in response to red_menace
    Level 1 (12 points)
    Mac OS X
    Jun 9, 2016 2:41 AM in response to red_menace

    hi,

     

    i had tried it but not get successed.

     

    1. on adding folder items to this_folder after receiving these_items -- folder action 
    2.   _main(these_items)
    3. end adding folder items to

    But not work this script as a folder action script.


    Thanks

    Rakesh


  • by red_menace,Helpful

    red_menace red_menace Jun 9, 2016 10:49 PM in response to rakeshsingh93
    Level 6 (15,541 points)
    Desktops
    Jun 9, 2016 10:49 PM in response to rakeshsingh93

    It would help if you explained what did not work.  I am just going to guess that you used the Folder Action Setup utility to enable folder actions and attach the script to a folder.

  • by rakeshsingh93,

    rakeshsingh93 rakeshsingh93 Jun 9, 2016 10:54 PM in response to red_menace
    Level 1 (12 points)
    Mac OS X
    Jun 9, 2016 10:54 PM in response to red_menace

    Hi,

    Yes you guess it right I used the Folder Action Setup utility to enable folder actions and attach this script to a folder.

    but it not work in folder action. nothing happend in Folder action

    here is code which i uploaded in Action setup utility

    on adding folder items to this_folder after receiving these_items

       

        repeat with d in these_items

           

            tell application "System Events"

               

                set p to properties of d

               

                if type identifier of p is "public.folder" then

                   

                    _main({d})

                   

                end if

               

            end tell

           

        end repeat

       

        on_main(argv)

        (*

            list argv : list of source directories

        *)

        script o

            property ff : {} -- list of aliases

            property gg : {} -- list of aliases to be moved per prefix

            property nn : {} -- list of file names

            property mm : {} -- list of prefixes

            property mm1 : {} -- list of distinct prefixes

           

            repeat with d in argv -- per source directory

                set d to d's contents

                -- retrieve list of aliases and names for document files

                tell application "Finder"

                    tell item d's document files to set {ff, nn} to {it as alias list, name}

                end tell

               

                -- retrieve prefixes from file names

                (*  E.g.

                        name   = AAA_BBB_CCC_DDD.tif

                        prefix = AAA_BBB_CCC

                *)

                try

                    set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {"_"}}

                    repeat with i from 1 to count my nn

                        set {n, m} to {my nn's item i, ""}

                        if (count n's text items) > 1 then set m to "" & n's text items 1 thru -2

                        set my mm's end to m

                        if m ≠ "" and m is not in my mm1 then set my mm1's end to m

                    end repeat

                    set AppleScript's text item delimiters to astid0

                on error errs number errn

                    set AppleScript's text item delimiters to astid0

                    error errs number errn

                end try

               

                -- distribute files per prefix

                repeat with m in my mm1 -- per distinct prefix

                    set m to m's contents

                    -- prepare destination for this prefix

                    try

                        set dst to (d as string) & m & ":" as alias

                    on error number -43 -- dst not present

                        tell application "Finder" to set dst to (make new folder at d with properties {name:m}) as alias

                    end try

                    -- collect sources for this prefix

                    repeat with i from 1 to count my mm

                        if my mm's item i = m then set my gg's end to my ff's item i

                    end repeat

                    -- move collected sources to destination

                    if gg ≠ {} then tell application "Finder" to move gg to dst with replacing

                    -- reset this collection

                    set gg to {}

                end repeat

            end repeat

        end script

        tell o to run

        end_main

    end adding folder items to

     

     

    Check and revert me your helpful support.

    where we need to change the code.

     

    Thanks

    Rakesh