Pregunta

I have searched all corners of the internet, including my connections, yet nobody knows much about keystroke events in AppleScript.

What I'm trying to accomplish is an AppleScript that adds printers by first asking for the variables such as the IP address an the location of the printer, then the script will open up the AddPrinter application that exists on all Macs and the script will then use simulated keystrokes to input all the previously set variables into the fields and click "Add" so that it adds the printer.

It should look something like this:

set ip_address to text returned of (display dialog "Enter Printer Ip Adress" default answer "" buttons {"OK"} default button 1)

set printer_name to text returned of (display dialog "Enter Name of Printer" default answer "" buttons {"OK"} default button 1)

set printer_location to text returned of (display dialog "Enter Location of Printer" default answer "" buttons {"OK"} default button 1)

tell application "AddPrinter" to activate

tell application "System Events"

    tell process "AddPrinter"

        tell window 1 -- or “window 1”

            click button "IP" of toolbar 1 -- or “button 3”

            tell combo box 2 of group 2 of group 1

                keystroke ip_address

            end tell

            delay 1

            tell group 1 of group 1

                set value of text field 1 to printer_name

                set value of text field 2 to printer_location

                -- you can't use the reserved word “location”

            end tell

        end tell

    end tell

end tell
¿Fue útil?

Solución

If you replace all the tell application "System Events" block with

do shell script "lpadmin -p " & ¬
    quoted form of printer_name & ¬
    " -L " & quoted form of printer_location & ¬
    " -E -v " & quoted form of ("lpd://" & ip_address) & ¬
    " -P " & "/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/PrintCore.framework/Versions/A/Resources/Generic.ppd"

under Mavericks it should work.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top