Printing a file location which is a Variable

Hi,

I am trying to send the file that i have extracted as a result then converted into a variable to the printer but it seems that i cannot. I manage to get Applescript to open the file but i cannot get it to send the file to the printer. Can anybody assist?

tell application "Daylite"
	eval "
		opportunity := selectedObjects lastObject.
		predicate := NSPredicate predicateWithFormat:'displayName = \"TheScreenShot\"'.
		attachments := opportunity fileAttachments.
		filteredAttachment := (attachments filteredArrayUsingPredicate:predicate) lastObject.
		
		filteredAttachment retrieveAttachmentWithDelegate:nil error:nil.
		
		localLocation := filteredAttachment cachedURL.
		 
	
		
		
		localLocation.
		
	"
	
	set theFile to result
	
end tell


tell application "HP Officejet Pro 276dw MFP"
	activate
	print location of the theFile

end tell

I end up with this error:

Can’t get location of "file://localhost/Users/************/Library/Caches/com.marketcircle.Daylite4/FileAttachments/B9B668B7-E687-4D74-B4BD-0B93EBCC863E/E/EC/Screen%20Shot%202015-06-21%20at%2000.48.44.png".

I just can’t seem to overcome this problem. The matter is very simple i have managed to find the url for the file i just need to send it to the printer. I am very confused.

Perhaps the confusion there is between two types of entity:

  • An actual file in the OS X file system
  • The string which represents a browser-type URL path to a file

The print function assumes that you are giving it a Unix reference to a file, so if you give it a URL string, as understood by a browser, it chokes.

Essentially, if it’s the path itself which you want to print, you need to throw your URL string into a temporary file, and then print that file.

One way, in Applescript, might be something like this (adjust printer name and temporary file name to taste):

on run {}
	
	printString("file:/// etc path", "Dell Color Laser 3110cn")
	
end run


on printString(strAny, strPrinterName)
	
	-- CREATE A THROW-AWAY FILE (CONTAINING YOUR STRING) ON THE DESKTOP
	set strPrintFilePath to POSIX path of (path to desktop) & "fileToPrint.txt"
	set strCMD to "echo " & quoted form of strAny & " > " & strPrintFilePath
	do shell script strCMD
	
	
	-- AND THEN PRINT THE THROW-AWAY FILE
	tell application strPrinterName
		activate
		print strPrintFilePath without print dialog
	end tell
	
end printString

And if (more likely :- ) it is the contents of the file, rather than its path, which you want to print, then you need to convert from the URL representation (“file://” etc, as used by browsers) to the unix style file path representation which OS X functions expect.

 "file://localhost/Users/robintrew/Desktop/Some%20file%20that%20we%20want%20to%20print.txt"

 → 

"/Users/robintrew/Desktop/Some file that we want to print.txt"

See How do I decode a URL using KBM?

It’s very unlikely the HP Officejet Pro printer utility wants a URL. It more likely wants either an alias or a Posix Path.

If you drop it on the Applescript Editor (Script Editor in Yosemite), and the app is scriptable you will be presented with its sdef (scripting dictionary). You can search for the “print” command, and what can be printed should be specified.

I say should, because it could just say something like “print reference” and not get specific.

Sdefs like man pages tend to be notoriously short of details.

-Chris

Hi, Thank you both for your response. I obtain the file that i want to print by running EVAL script above. Then i have converted that response of the URL into a variable. I am trying to send the whole file in the variable location to the printer.

‘Complex Print’ I really don’t know how to implement the script that you sent over. Sorry i am not really a programmer i am a begginer. The intentions are to print the file that i get as a result, which is in this case ‘localLocation.’ but the variable ‘theFile’.

First experiment:

  1. Delete your code between tell application "HP Officejet Pro 276dw MFP" and end tell
  2. Replace with the code below
  3. Run
-- tell application "HP Officejet Pro 276dw MFP"
-- 	activate
-- 	print location of the theFile
-- 
-- end tell

set strCMD to "osascript -l JavaScript -e 'decodeURI(\"" & theFile & "\").slice(7)' "
set strPosixPath to (do shell script strCMD)

set strPrinterName to "HP Officejet Pro 276dw MFP"

tell application strPrinterName
	print strPosixPath
end tell

PS this approach to converting a file:// url string to a posix path requires Yosemite’s Javascript for applications, if you are running an earlier OS X, one of the methods at How do I decode a URL using KBM? can be used instead.

PS it occurs to me that earlier versions of OS X do contain a basic Javascript interpreter (jsc), so if you are not running Yosemite, you could try:

set strCMD to ¬
"/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e " & ¬
	"'print(decodeURI(\"" & theFile & "\").slice(7))'"
set strPosixPath to (do shell script strCMD)

set strPrinterName to "HP Officejet Pro 276dw MFP"

tell application strPrinterName
	print strPosixPath
end tell

Hi ComplexPoint, thank you again for your response and continued assistance. I am running the latest version of Yosemite 10.10.3. I have attempted the first experiment and the no error message was returned, however nothing at all printed. I assume that something is now missing. What could it be? no error message but nothing else printed either.

We should probably have added without print dialog after print strPosixPath (as below)

set strCMD to ¬
"/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e " & ¬
	"'print(decodeURI(\"" & theFile & "\").slice(7))'"
set strPosixPath to (do shell script strCMD)

set strPrinterName to "HP Officejet Pro 276dw MFP"

tell application strPrinterName
	print strPosixPath without print dialog
end tell

Hi,

This time I get the error message

Expected “into”, variable name, class name, other parameter name or property but found command name.

but i don’t get the error message when use:

set strCMD to ¬
		"/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -e " & ¬
		"'print(decodeURI(\"" & theFile & "\").slice(7))'"
	set strPosixPath to (do shell script strCMD)
	
	set strPrinterName to "HP Officejet Pro 276dw MFP"
	
	tell application strPrinterName
		print strPosixPath without dialog
	end tell

by the way could we use NSPrintOperation here?

Hi, could I ask you to zip the whole of your code and either post it here or send it to me in a forum message ?

( Probably a bit quicker to test and fix over here )