14 Replies Latest reply on Apr 8, 2011 2:01 AM by heavyboots

    Need Help Creating A Script For Labeling Finder Files from Indesign

    jimfelder

      Trying to figure out a way to have a script run in InDesign CS4 that will label a text file RED after it's been imported into InDesign.

      We import numerous text files into an InDesign template. Since there are several text files in the same folder, we need to somehow "mark off" that we've imported that file and move to the next one to import. I have created an Automator "service" with a keyboard shortcut to label the file Red in the finder but it would be great if we could have this happen automatically after we import into InDesign.

       

      If we import the wrong text file by accident and it gets missed in proofing, it costs us a lot of money with the printer.

       

      Thanks for any ideas.

       

      Jim

        • 1. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
          Muppet Mark-QAl63s Level 4

          Kasyan, has a script that does this posted on his site… I think it used to only be possible via AppleScript but the newer versions of OSX have shell too. Handier for use with Bridge & Photoshop…

          1 person found this helpful
          • 2. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
            jimfelder Level 1

            Can you provide a link? I don't know who Kasyan is. Thank you for the info.

            • 3. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
              jimfelder Level 1

              I found his site but not finding a script for what I'm needing. Do you?

              • 4. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                Kasyan Servetsky Level 5

                Here is a example that sets labels to PDF file I create via Distiller: it triggers AS via doScript method. I often use this trick in my scripting practice.

                main();
                
                function main() {
                     if (app.extractLabel("LastPdfFiles").split(",") != "") {
                          var myPDFs = app.extractLabel("LastPdfFiles").split(",");
                          for ( i = 0; i < myPDFs.length; i++) {
                               setLabel(myPDFs[i], 1);
                               File(myPDFs[i]).execute();
                          }
                     }
                }
                
                function setLabel(myPath, myLabel) {
                     // myLabel: 0 – none, 1 – orange, 2 – red, 3 – yellow, 4 – blue, 5 - violet, 6 - green, 7 – gray 
                     if (File.fs == "Macintosh") {
                          if (new File(myPath).exists) {
                               var myMacPath = myPath.replace("/", "").split("/").join(":");
                               var myScript = 'tell application \"Finder\"\r';
                               myScript += 'set myFile to file \"' + myMacPath + '\" as alias\r';
                               myScript += 'set label index of myFile to '+ myLabel + '\r';
                               myScript += 'end tell\r';
                               app.doScript(myScript, ScriptLanguage.applescriptLanguage);
                          }
                     }
                }
                

                 

                I don't have time to try this myself, but probably you might to write a similar script and attach it to "afterImport" event.

                Example:

                #targetengine "session"
                myAfterImportScriptPath = app.filePath + "/Scripts/Scripts Panel/AfterImportScript.jsx";
                var myAfterImportScript = new File (myAfterImportScriptPath);
                if (myAfterImportScript.exists) {
                     var myAfterImportEventListener = app.addEventListener( "afterImport", myAfterImportScript, false );
                }
                

                 

                Though I am not sure how to get reference to the file which is being imported.

                 

                Kas

                • 5. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                  jimfelder Level 1

                  I really appreciate the response and help Kasyan, I did find a script on line that allowed me to label a finder item. But I don't know how to create Applescripts or modify them. I just google and find things that might work. I can sometimes figure out little bits to fit my needs but I have no clue on the proper terms or codes or commands.

                   

                  Thanks

                  • 6. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                    Kasyan Servetsky Level 5

                    You can use the pre-installed Script Editor to write and debug Apple scripts, but I recommend you Script Debugger instead because it's much better.

                    I also recommend you to begin with "Apple script for absolute starters" by Bert Fishcher. It's a free book, you can google for it, or I can send you a copy.

                     

                    Here is a very simple example of labeling a file in AS:

                    tell application "Finder"
                         set theFile to a reference to file "Data:Sample.txt"
                         set label index of theFile to 2
                    end tell
                    

                     

                    Kas

                    • 7. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                      heavyboots Level 2

                      At its heart, this is what you want to do.

                       

                      tell application "Adobe InDesign CS4"
                           set placeFile to choose file
                           tell active document
                                tell selection
                                     --Assumes you have preselected the picture or textbox to place file in
                                     place placeFile
                                end tell
                           end tell
                      end tell
                      tell application "Finder"
                           set label index of placeFile to 2
                      end tell
                      
                      

                       

                      You can expand on that pretty easily. The scripting dictionary for InDesign shoudl be in the Library palette in the Applescript Editor and the docs for Applescript are all online.

                       

                      If you need more assistance, you'll probably want to request someone to write a full script for you to spec for actual moneyz.

                       

                      Best o' luck!

                      Eric.

                      • 8. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                        John Hawkinson Level 5

                        Kasyan wrote:

                        I don't have time to try this myself, but probably you might to write a similar script and attach it to "afterImport" event.

                        Example:

                        #targetengine "session"
                        myAfterImportScriptPath = app.filePath + "/Scripts/Scripts Panel/AfterImportScript.jsx";
                        var myAfterImportScript = new File (myAfterImportScriptPath);
                        if (myAfterImportScript.exists) {
                             var myAfterImportEventListener = app.addEventListener( "afterImport", myAfterImportScript, false );
                        }
                        

                         

                        Though I am not sure how to get reference to the file which is being imported.

                         

                        Oh, that's easy though it's probably better not to use a seperate script file because arguments get confusing. Just keep it with a function. Check out my exportPop.jsx script, but basically:

                         

                            app.addEventListener("afterImport", function(ev) {
                                alert(ev.fullName);
                            });
                        
                        

                         

                        (feel free to name the function though if that makes it easier)

                        • 9. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                          Kasyan Servetsky Level 5

                          Thanks for the tip, John. I just thought it would be easier to write and debug it as a standalone script first and then attach it to the event listener.

                           

                          Kas

                          1 person found this helpful
                          • 10. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                            jimfelder Level 1

                            wow you guys rock! I truly appreciate you guys writing and providing scripts. I didn't expect anyone to actually write scripts. I was just hoping someone else had done this or figured out a way.

                             

                            thanks again, I'll see what I can do with what you've provided.

                            • 11. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                              jimfelder Level 1

                              Eric! Wow this is exactly what we needed. You brainy code guys just amaze me. THANK YOU THANK YOU THANK YOU!

                              • 12. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                                heavyboots Level 2

                                You know, these guys are absolutely right in their assessment that this is a good place for event listeners. You would need three scripts though.

                                 

                                Something like...

                                 

                                Label Placed Files Red.scpt

                                --Put this in a folder called IDScripts in your Documents folder

                                main(evt)

                                on main(myEvent)

                                tell application "Adobe InDesign CS4"

                                set placedFilePath to full name of myEvent

                                set placedFilePath to placedFilePath as alias

                                end tell

                                 

                                tell application "Finder"

                                --This turns the file color red

                                set label index of placedFilePath to 2

                                end tell

                                end main

                                 

                                And these two are in your InDesign scripts folder...

                                 

                                Start Placed File Finder Labelling.scpt

                                --Installs the afterImport event listener for the Label Placed Files Red script.

                                tell application "Adobe InDesign CS4"

                                set myHandler to path to documents folder

                                set myScriptName to "Label Placed Files Red.scpt"

                                set myHandler to "" & myHandler & "IDScripts:" & myScriptName

                                try

                                set myHandler to myHandler as alias

                                make event listener with properties {event type:"afterImport", handler:myHandler, captures:true}

                                display dialog "Placed File Labelling: ON" buttons "OK" default button 1

                                on error

                                display dialog "ERROR: Couldn't start handler!" & return & return & "Make sure the \"" & myScriptName & "\" script in a folder called \"IDScripts\" inside your Documents folder and try again" buttons "OK" default button 1

                                 

                                end try

                                end tell

                                 

                                Stop Placed File Finder Labelling.scpt

                                --Removes the afterImport event listener for the Label Placed Files Red script.

                                tell application "Adobe InDesign CS4"

                                set myHandler to path to documents folder

                                set myScriptName to "Label Placed Files Red.scpt"

                                set myHandler to "" & myHandler & "IDScripts:" & myScriptName

                                set myHandler to myHandler as alias

                                set myResult to remove event listener event type "afterImport" handler myHandler with captures

                                if (myResult is false) then

                                display dialog "ERROR: Couldn't remove handler! Are you sure it is active?" & return & return & "If so, please quit and relaunch InDesign to get rid of it." buttons "OK" default button 1

                                else

                                display dialog "Placed File Labelling: OFF" buttons "OK" default button 1

                                end if

                                end tell

                                • 13. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                                  John Hawkinson Level 5
                                  You know, these guys are absolutely right in their assessment that this is a good place for event listeners. You would need three scripts though.

                                  Not really. My usual paradigm for event listeners in JavaScript is that the script checks to see if the listener is already installed -- if so, it removes it. If not, it installs it. And it installs a handler that's in the same script. So one script. I don't think there's some subtle reason why AppleScript needs seperate scripts...

                                  • 14. Re: Need Help Creating A Script For Labeling Finder Files from Indesign
                                    heavyboots Level 2

                                    Good to know. Basically, I read Ch 7 of the Scripting Guide over lunch and that was their recommendation...

                                     

                                    Heard about them, never got around to trying them when I was doing a lot of ID scripting.