I’m very new to applescript and have been pulling together an applescript app that will install and configure printers for my helpdesk technicians. I’m having some trouble getting file names with spaces to work correctly in two regions of my script. the file names are passed to the scripts using variables so that I can keep the script cleaner and more universal for the multiple printers I need to setup.
Here’s the apple script app:
-- This applescript application will copy the bundled printer PPD driver files and install a specified printer with all the appropriate options for use.
-- Script Created by MD
-- Script Updated on July 3, 2014
-- This will prompt user to confirm the installation of the printer and performs the printer installation.
set question to display dialog "This application will install and configure a new Xerox Printer on this computer. Please click Install if you would like to continue." buttons {"Cancel", "Install"} default button 2
set answer to button returned of question
-- If user cancels the installation then application quits
if answer is equal to "Cancel" then
quit
-- If user chooses Install then application prompts user to choose printer to install
else if answer is equal to "Install" then
--Ask user to choose printer from list and confirm
(choose from list {"CPX1", "CPX2", "CPX3"} ¬
with prompt "Please choose the name of the printer you'd like to install and click OK. Please note that after you click OK you will be prompted to enter your password in order for this printer installation to contine.")
set theprinterdisplayname to result as text
-- sets the PrinterModel, and printerlocation based on theprinterdisplayname variable
if theprinterdisplayname is equal to "CPX1" then
set PrinterModel to "5855"
set PrinterLocation to "Department_Hallway"
else if theprinterdisplayname is equal to "CPX2" then
set PrinterModel to "5855"
set PrinterLocation to "VP_Office"
else if theprinterdisplayname is equal to "CPX3" then
set PrinterModel to "7855"
set PrinterLocation to "Back_Hallway"
else if theprinterdisplayname is equal to "CPX4" then
set PrinterModel to "5855"
set PrinterLocation to "Facility_Office"
end if
-- sets the PPD file, resource location path, printer model specific printer options, based on PrinterModel variable
if PrinterModel is equal to "5855" then
set PPDFile to "Xerox WorkCentre 5855.gz"
set resourcePath to POSIX path of (path to resource "" & PPDFile & "")
set ModelOptions to "-o PageSize=Letter -o XRTrays=FiveTraysHCTT -o XRHolePunch=TruePunch -o XRLanFax=False -o XRJobStorage=True -o XRFinisher=OF -o XRAccountingSystem=XSA -o XRAccountingPrompt=Prompt -o XRAccountingMaskUserID=False"
else if PrinterModel is equal to "7845" then
set PPDFile to "Xerox WorkCentre 7845.gz"
set resourcePath to POSIX path of (path to resource "" & PPDFile & "")
set ModelOptions to "-o PageSize=Letter -o XRTrays=FiveTraysHCTT -o XRHolePunch=23Unit -o XRLanFax=False -o XRJobStorage=True -o XRFinisher=AFinisher -o XRAccountingSystem=XSA -o XRAccountingPrompt=Prompt -o XRAccountingMaskUserID=False"
else if PrinterModel is equal to "7855" then
set PPDFile to "Xerox WorkCentre 7855.gz"
set resourcePath to POSIX path of (path to resource "" & PPDFile & "")
set ModelOptions to "-o PageSize=Letter -o XRTrays=FiveTraysHCTT -o XRHolePunch=23Unit -o XRLanFax=False -o XRJobStorage=True -o XRFinisher=ProfessionalFinisher -o XRAccountingSystem=XSA -o XRAccountingPrompt=Prompt -o XRAccountingMaskUserID=False"
else if PrinterModel is equal to "7775" then
set PPDFile to "Xerox WorkCentre 7775.gz"
set resourcePath to POSIX path of (path to resource "" & PPDFile & "")
set ModelOptions to "-o PageSize=Letter -o XRTrays=FiveTraysHCTT -o XRHolePunch=TruePunch -o XRLanFax=False -o XRJobStorage=True -o XRFinisher=ProfessionalFinisher -o XRAccountingSystem=XSA -o XRAccountingPrompt=Prompt -o XRAccountingMaskUserID=False"
end if
-- Sets POSIX path of the PPD file that will be used with the printer on target system.
set PPDTargetPath to POSIX path of ("/Library/Printers/PPDs/Contents/Resources/") & "" & PPDFile & ""
-- Sets path to location where PPD files are stored on target system
set PPDPath to "/Library/Printers/PPDs/Contents/Resources/"
-- This is the shell script that will be run to install and configure the the printer. It will be called later.
set installscript to "/usr/sbin/lpadmin -p '" & theprinterdisplayname & "' -v 'lpd://" & theprinterdisplayname & ".domain.com/lp' -D '" & theprinterdisplayname & "' -L '" & PrinterLocation & "' -P '" & PPDTargetPath & "' -E " & ModelOptions & ""
-- Test to see if Driver file is already installed then depending on result copy driver file and install printer or just install printer.
tell application "Finder"
if exists PPDTargetPath as POSIX file then
set PPDExists to true
else
set PPDExists to false
end if
end tell
if PPDExists is equal to false then
do shell script "cp -R -n -p " & resourcePath & " " & PPDPath & "" with administrator privileges
-- Pause to wait for file to copy
delay 3
do shell script installscript
quit
else if PPDExists is equal to true then
do shell script installscript
quit
end if
end if
When I run the above applescript app i get an error the when the CP command is run to copy the driver file. The error reads: "cp: /users/user/documents/scripts/completed/testscript.app/Contents/Resources/Xerox: No such File or directory cp: WorkCentre: No such file or directory cp: 5855.gz: No such file or directory
The spaces in the file name are not being interpreted correctly when the script executes and creates the resourcePath variable below:
if PrinterModel is equal to "5855" then
set PPDFile to "Xerox WorkCentre 5855.gz"
set resourcePath to POSIX path of (path to resource "" & PPDFile & "")
set ModelOptions to "-o PageSize=Letter -o XRTrays=FiveTraysHCTT -o XRHolePunch=TruePunch -o
and then passes it to the cp command.
do shell script "cp -R -n -p " & resourcePath & " " & PPDPath & "" with administrator privileges
I’m not sure if this is a well written code or not but it does all work exactly as intended if the PPDfile variable has no spaces in the filename. I however need it to work with spaces in the filename.
I’ve tried various combinations of escaping the spaces with a \ or a \ but all of them seem to cause problems in one spot or another.
Can anyone see the error of my ways??
Thanks in advance for the help.
Matt