Applescript, help Printing All Files in a Folder as Client

Discussion in 'Mac Programming' started by Insector X, Jun 8, 2016.

  1. Insector X macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #1
    Hi,

    I just signed up! I've been trying to find this answer, but I think I'm going to have to just ask. [​IMG] Hello All.

    This folder I'm trying to print from a folder that is on a server that I'm connected to via afp over tcp/ip. The folder is called "Network Printer ƒ" and is on the root directory of "Macintosh HD" on the Server "Macintosh 8600" [​IMG] (My Snow Leopard Mac Mini connects to this server automatically at startup)

    What I want to do is have an application/script that i can open with a hotkey on this computer, to automatically print from "Network Printer ƒ" to my default printer. I tried to do this in Automator, but couldn't figure it out. A folder action on this "Network Printer ƒ" didn't work 95% of the time. Considering that my Mac didn't check that networked folder on the server for updates often enough, it seems. I would prefer to have this process fully automated, rather than using a hotkey. But I'm running out of ideas. Any other ideas are also welcome.

    Thank You in advance!

    ---------------------------

    Mac Mini 2006 2 Ghz Core2Duo 1GB RAM
    Snow leopard 10.6.3 fresh install (I plan on updating to 10.6.8 soon)

    PS- I know this is an old machine, but I'm using it for a project. I already have a newer Mac for other stuff.
    So, I have no plans to upgrade this machine any further.
     
  2. Insector X, Jun 8, 2016
    Last edited: Jun 8, 2016

    Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #2
    tell application "Finder"
    print every item of "Volumes:Macintosh HD:Network Printer ƒ"
    end tell

    this didn't work, but maybe it'll help you all see where I'm trying to go with this

    Or should the folder hierarchy read: "Macintosh 8600:Macintosh HD:Network Printer ƒ"
    (this didn't work either)
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    The first one won't work because the pathname is the wrong form. You have mixed Posix path form ("Volumes") with classical Mac OS form ("Macintosh HD:Something").

    The second one won't work because classical Mac OS forms aren't prefixed by server names.


    Do this.

    1. Open a Terminal window.
    2. Type the following, WITHOUT pressing RETURN or ENTER at the end:
    Code:
    ls -l 
    Note there's a space after the "-l".
    More than one space is fine, but there must be at least one space.

    3. Next, switch to Finder.
    4. Drag the folder of "Network Printer ƒ" on top of the Terminal window, and drop it.
    5. You should see Terminal "magically" type the full pathname of the folder.
    6. Switch back to the Terminal window and press RETURN.
    7. The result should be a list of the files in the folder.

    If you don't see a listing of the files, then copy and paste the complete command-line you typed, with the folder name, and the complete error message, and post it in a reply here.

    The pathname that magically appeared in the Terminal window is the Posix path form. It starts with "/", then "Volumes", and it uses "/" between folder components.

    Try this in your AppleScript:
    Code:
    set printThisFolder to Posix file "EXACT_PATHNAME_GOES_HERE"
    tell application "Finder"
       print every item of ( printThisFolder as alias )
    end tell
    Where I've typed EXACT_PATHNAME_GOES_HERE, copy the exact pathname from the Terminal window, and paste it between the quotes. You can drag to select the text in Terminal, then ⌘C to copy to the clipboard; then switch to AppleScript Editor (or whatever) and paste the text into place.


    If you want to see what the classical Mac OS form looks like, run this single line in AppleScript Editor, and look at the result.
    Code:
    set printThisFolder to Posix file "EXACT_PATHNAME_GOES_HERE" as alias
    
    Be sure to use the actual exact pathname, pasted in from Terminal.
     
  4. Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #4

    Thanks for the great reply, This is working good for a lot of files:

    set printThisFolder to POSIX file "/Volumes/Macintosh_HD/Network_Printer_ƒ"
    tell application "Finder"
    print every item of (printThisFolder as alias)
    end tell

    But I'm having trouble with this working with .eps files. What happens is the script opens preview, then preview converts the postscript. And I can't see the converted picture anywhere, until I try to quit Preview. Then, it asks me if i want to save. And doesn't print.

    75% of what i'm going to be printing is going to be .eps files.

    set printThisFolder to POSIX file "/Volumes/Macintosh_HD/Network_Printer_ƒ"
    tell application "Finder"
    print every item of (printThisFolder as alias)
    end tell
    tell application "Preview"
    delay 15
    quit
    end tell

    What this does with .eps files is: Opens Preview, then waits 15 seconds, then tries to quit and asks me if i want to Save. But if I push cancel at the save prompt then it finally shows me the .eps image in preview, and i can then manually print. It seems like if i can maybe try the quit command, then enter cancel, then print, then quit again, then "don't save". But I'm sure there's got to be a simpler way. Any ideas?

    How do you select "cancel" or "don't save" from a pop up save menu, through applescript?

    Maybe I need to write two different scripts for different file types. I dunno.

    Thanks again, this has been rewarding :)
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Clicking "Cancel" or "Don't Save" by scripting is not the answer. Picking the right application to tell "print" to is likely to work much better. The "print" command is a standard one for apps, and most apps will support it.

    There are different ways of approaching this. You could get a list of every file in printThisFolder whose extension is "eps", and then directly tell the correct app to print those files.

    First, you'll have to tell us what app you want to print EPS files with.

    I won't be able to post another reply for a few hours, I'm heading out soon. Someone else might answer in the interim, or it'll have to wait until I'm free.
     
  6. Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #6
    I've been doing some research about what would be a good free program that can handle this task. I found that Gimp can print eps files using the GhostScript plugin. Have you heard of this? Is there any better options you may recommend? This Mac Mini I'm on has a fresh install of 10.6, so I don't currently have an application on my computer that I'm wanting to use to print eps.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    I've heard of Gimp and GhostScript, but never used either one. I have no suggestions on apps that can print EPS.

    There is a Terminal command that will convert EPS to PDF. After that conversion, Preview should able to print the PDF. Let me think about a good way to arrange that on 10.6.

    Please answer the following:
    1. What do you intend to do with the EPS files after printing them?
    2. If a PDF file is created in the same location as the EPS file, is it necessary to delete it after printing?
    3. What file types other than EPS have to be printed?
     
  8. Insector X, Jun 9, 2016
    Last edited: Jun 9, 2016

    Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #8
    1. It would be nice if they would delete after printing, so long as i can be sure they've printed.
    2a. If we were going that direction, I'd say yes
    2b. I'm sure I can change my original file output from EPS to PDF without issues. This sounds like an easier alternative, than converting the file. (I run the server)
    3. .txt, .jpg, .gif, .png I think that's it.

    Is it safe for me to automatically have my mac open this applescript app periodically? What I mean is: what if it opens the applescript while a file hasn't fully transfered, or something? Will it give me errors?
     
  9. Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #9
    I switched to PDF and everything is printing fine now. Thanks so much Chown33! :)

    The only thing left to make this perfect is for me to find a way to maybe make this into a "stay open" script that runs every 2 minutes or so.
     
  10. Insector X, Jun 10, 2016
    Last edited: Jun 11, 2016

    Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #11
    I'm trying to turn this (great working thanks to chown33) applescript to idle and repeat:

    Code:
    set printThisFolder to POSIX file "/Volumes/Macintosh_HD/Network_Printer_ƒ"
    tell application "Finder"
       print every item of (printThisFolder as alias)
       delay 22
       delete every item of (printThisFolder as alias)
    end tell

    This is the idea I'm messing with but i have the idle stuff all wrong. How do i properly format this mess?:

    Code:
    set printThisFolder to POSIX file "/Volumes/Macintosh_HD/Network_Printer_ƒ"
    tell application "Finder"
    on idle
       print every item of (printThisFolder as alias)
       delay 22
       delete every item of (printThisFolder as alias)
       return 90
       end idle
    end tell
    EDIT: I feel that this question is different enough from the original question, that I should maybe start a new thread. What do you guys think?
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    You marked the thread "Resolved", so I thought it was, well, resolved. If it's unresolved, please unmark it:
    http://forums.macrumors.com/threads/forum-feature-resolved-thread-prefix.1966177/

    Also, please use CODE tags when you post code. I've been asking the moderators to add CODE tags, but you can do it yourself and simplify things.
    http://forums.macrumors.com/threads/posting-code-please-use-the-code-tags.1356447/


    First, read the following section of "AppleScript Language Guide" on AppleScript handlers. Ideally, you should read the whole Guide, but this section shows how to write an idle handler:
    https://developer.apple.com/library...//apple_ref/doc/uid/TP40000983-CH206-CJBIDBJH

    Next, look at your code.

    The first thing to notice is that your on idle block is contained inside a tell block. The linked section on handlers says you can't nest handlers ("Handler Basics" heading). So the first thing to change is to rearrange the tell and on idle to this:
    Code:
    set printThisFolder to POSIX file "/Volumes/Macintosh_HD/Network_Printer_ƒ"
    on idle
      tell application "Finder"
        print every item of (printThisFolder as alias)
        delay 22
        delete every item of (printThisFolder as alias)
      end tell
      return 90
    end idle
    
    At this point, the script has an implicit run handler, consisting of all the script lines outside of any handler. Read the subsection on run handlers in the above link.

    I don't think this code will work as is, but try it and see what happens. Trying things to confirm (or refute) one's expectations is an essential part of programming. Get used to doing it.


    Here's another section on variables:
    https://developer.apple.com/library...tml#//apple_ref/doc/uid/TP40000983-CH223-SW10

    I think the scope of the variable printThisFolder is the implicit run handler, so it won't be in the scope of any explicit handlers, i.e. the idle handle. This is why I don't think the above code will work as given, but I could be wrong.

    A simple solution is to define printThisFolder as a property. Like this:
    Code:
    property printThisFolder : POSIX file "/Volumes/Macintosh_HD/Network_Printer_ƒ"
    
    on idle
      tell application "Finder"
        print every item of (printThisFolder as alias)
        delay 22
        delete every item of (printThisFolder as alias)
      end tell
      return 90
    end idle
    
    I think this will work, so try it and see what happens.

    If it doesn't work as expected, do this:
    1. Describe what you expected to happen.
    2. Describe what actually happened.
    3. Post any error messages, exactly.

    You should test what happens if the folder is empty. I'm not sure what happens if an empty list results from an every item of clause.

    You should also test what happens if there's a subfolder in the folder.

    And also test what happens if the folder doesn't exist because the server isn't connected.

    Handling contingencies is a big part of writing programs that work. Another thing to get used to doing.
     
  12. Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #13

    Sorry about the "resolved status" confusion. I jumped the gun. I'm a little new to forums and their etiquette. Also, I will start using the code function.

    I will read those applescript learning resources you posted, Thank you for that.

    I will try that code you posted, and test all the things you listed. It may be a day or two, because this computer/server is at my work. But I'll try to go in sometime this weekend to take care of this.

    Thanks you again for all your help
     
  13. Insector X, Jun 12, 2016
    Last edited: Jun 14, 2016

    Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #14

    It's almost perfect! Awesome. Here's the answers to your questions:

    1. When the script opens I want it to print .pdf, .txt, .rtf, .jpg, .gif, .png fully automated. And not leave any applications open when it's done. I want it to use either Preview or TextEdit to print (which it is already doing automatically on a fresh install of Snow Leopard)

    2. It printed everything perfectly! Although, it did leave text edit open while printing an .rtf file, and it left preview open when printing a .pdf. Is there a way to automatically quit these programs as part of the idle loop? (I'm sure there is. I'm gonna play around with it).
    EDIT: Success, I got these programs to quit automatically as part of the idle loop

    3. I did get an error when I tested losing the connection to the server. I got this error:

    Can't make file "Mac
    Mini:Volumes:Macintosh_HD:Network_Printer_ƒ" into
    type alias.

    also, if the script tries to delete any file in that folder before it's done printing, i get an error about the file being in use. I'll write the exact error tomorrow when i get back to my Mac Mini at work. And, if I put a file in the folder during the delay 22 after the print, then my file deletes without printing. I can shorten the delay time, but that increases the chance that I'll get an error about deleting a file that's currently in use.
     
  14. Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #15
    I've been messing around with the script, and this is what I have so far.

    Code:
    property printThisFolder : POSIX file "/Volumes/Macintosh_HD/Network_Printer_ƒ"
    on idle
       delay 15
       tell application "Finder"
         print first item of (printThisFolder as alias)
         delay 1
         delete first item of (printThisFolder as alias)
       end tell
     tell application "Preview"
         quit
       end tell
       tell application "TextEdit"
         quit
       end tell
       return 50
    end idle
    I still get the error message posted in my last message, about there being no server (if there is no server)
    I'm having a little trouble today reproducing the other error, but I've made a few workarounds in the script.
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    I would expect that if there's no server, then an error would occur.

    What do you want to happen instead?
     
  16. Insector X, Jun 15, 2016
    Last edited: Jun 15, 2016

    Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #17
    I want it to ignore the error (and keep looping and checking for files.)
     
  17. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #18
    You have basically two options:

    1. Check for the existence of the folder before attempting to print or delete items from it.
    Google search terms: applescript file exists
    Example result:
    http://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence

    2. Use an AppleScript try ... on error block and catch errors when they happen.
    https://developer.apple.com/library...riptLangGuide/reference/ASLR_error_xmpls.html
     
  18. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #19
    You can list jobs queued by the current user or show printer queue status with the lpstat or lpq commands. You can use them with a do shell script command e.g :

    Code:
    do shell script "lpstat"
    Example :

    Code:
    -- Print stuff
    repeat until (do shell script "lpstat") is ""
        delay 1
    end repeat
    -- Delete stuff
    Info : https://developer.apple.com/legacy/.../Darwin/Reference/ManPages/man1/lpstat.1.html - https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/lpq.1.html
     
  19. Insector X thread starter macrumors newbie

    Insector X

    Joined:
    Jun 8, 2016
    #20
    I will try this, although option 2 looks the most appealing to me atm

    I will try this as well. Probably after fathers day. Thanks again. you rock
     

Share This Page