Droplet script - Open File in Native Application

If I disable the instructions dedicated to MultiAd, the script displays the dialog “want to save .”
So my understanding is that the problem is the syntax of the open instruction.

May you run the script below ?

It’s not a droplet but an applet.
When you run it it will ask you to choose a crtr file.
As I disabled the instructions supposed to trap the error, you will be able to see which instructions are correctly executed and where is the one failing.

set myLog to (path to desktop as text) & "myLog_goLym.txt"


set aFile to choose file with prompt "Choose a "crtr" file."
set {name:fileName, name extension:fileExtension} to info for aFile
my writeto(myLog, "fileName = " & fileName & linefeed & "fileExtension = " & fileExtension & linefeed, text, false)
if fileExtension = "crtr" then
	if (count fileName) > 12 then
		set baseName to text -12 thru -6 of fileName
		my writeto(myLog, "point 1" & linefeed, text, true)
	else
		set baseName to text 1 thru -6 of fileName
		my writeto(myLog, "point 2" & linefeed, text, true)
	end if
	my writeto(myLog, "baseName = " & baseName & linefeed, text, true)
	
	set PDFPath to "mymountedVolume:"
	
	tell application "MultiAd Creator Pro"
		activate
		--try
		# aFile is an alias so there is no need to explicitely define the class
		--open (aFile as alias)
		my writeto(myLog, "point 3" & linefeed, text, true)
		open aFile
		my writeto(myLog, "point 4" & linefeed, text, true)
		set theDoc to document 1
		my writeto(myLog, "point 5" & linefeed, text, true)
		set destFolder to PDFPath
		my writeto(myLog, "point 6" & linefeed, text, true)
		# Assuming that the filename match the original asker description,
		# you are trying to export into : "mymountedVolume:1234567:myfilename1234567.crtr.pdf"
		# Is it really what you want.
		my writeto(myLog, "point 7, want to save in :" & linefeed & (destFolder & baseName & ".pdf") & linefeed, text, true)
		display dialog "want to save in :" & linefeed & (destFolder & baseName & ".pdf")
		--tell application "MultiAd Creator Pro" # No need for that, you are already speaking to this application
		(*	
                        export PDF current spread of theDoc saving in (destFolder & baseName & ":" & fileName & ".pdf") print order user order export method composite output color space CMYK stand in resolution 300 black and white compression use zip color image compression use JPEG quality 100 with embed no base 14 fonts, use doc size, binary encoding and compress text and line art without print as spreads, crop marks, registration marks, color bars, document notes, plate information, text blocks only and presentation mode
						
					*)
		-- end tell
		my writeto(myLog, "point 8" & linefeed, text, true)
		close front document saving no
		my writeto(myLog, "point 9" & linefeed, text, true)
		(*
				on error e
					display dialog "an error occured " & e
				end try
				*)
	end tell
	
end if


#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

#=====

It will create a text file named “myLog_goLym.txt” on the desktop.
If everything behaves flawlessly the text file will contain something like :
[format]fileName = myfilename1234567.crtr
fileExtension = crtr
point 1
baseName = 1234567
point 3
point 4
point 5
point 6
point 7, want to save in :
mymountedVolume:1234567.pdf
point 8
point 9[/format]

If all works well you will also get a dialog displaying something like :

want to save in :
mymountedVolume:1234567.pdf

If you post what you really get I will be able to know which instruction failed.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 18 mai 2017 17:21:15

my result:

fileName = highland meadows 2x2.5 1977011
fileExtension = missing value

Creator has the the ability to save with or without file extensions. My group saves without.

Would it be simple enough to have the script add .crtr to the file name after it is drop on the droplet??

It would be easy to edit the script to work with file which have no extension name.
The problem is to find a way to be sure that the passed file is really a crtr one.

I’m not sure of the meaning of what you wrote.
Is the file save with no extension or is it with the extension hidden ?

May you run this script and post the content of the file “myLog2_2goLym.txt” which will be created on the desktop ?
I hope that one of the returned values will give a way to check the nature of the passed file.

set aFile to choose file with prompt "choose a file of type crtr"

set myLog to (path to desktop as text) & "myLog2_2goLym.txt"

my writeto(myLog, "with System Events", text, false)
tell application "System Events" to tell disk item (aFile as text)
	set itsName to its name
	my writeto(myLog, "itsName = " & itsName, text, true)
	set itsDisplayedName to its displayed name
	my writeto(myLog, "itsDisplayedName = " & itsDisplayedName, text, true)
	set itsTypeID to its type identifier
	my writeto(myLog, "itsTypeId = " & itsTypeID, text, true)
	set itsExtension to its name extension
	my writeto(myLog, "itsExtension = " & itsExtension, text, true)
	set itsKind to its kind
	my writeto(myLog, "itsKind = " & itsKind, text, true)
end tell

my writeto(myLog, "with Finder", text, true)
tell application "Finder"
	set itsName to name of aFile
	my writeto(myLog, "itsName = " & itsName, text, true)
	set itsDisplayedName to displayed name of aFile
	my writeto(myLog, "itsDisplayedName = " & itsDisplayedName, text, true)
	set itsExtension to name extension of aFile
	my writeto(myLog, "itsExtension = " & itsExtension, text, true)
	set itsKind to kind of aFile
	my writeto(myLog, "itsKind = " & itsKind, text, true)
	set itsExtensionHidden to extension hidden of aFile
	my writeto(myLog, "itsExtensionHidden = " & itsExtensionHidden, text, true)
end tell

Here is a temporary version of the “main” script.
It is supposed to be able to treat files with the extension as well as files without it.

set myLog to (path to desktop as text) & "myLog_goLym.txt"


set aFile to choose file with prompt "Choose a "crtr" file."
set {name:fileName, name extension:fileExtension} to info for aFile
my writeto(myLog, "fileName = " & fileName & linefeed & "fileExtension = " & fileExtension & linefeed, text, false)
if fileExtension = "crtr" then
	if (count fileName) > 12 then
		set baseName to text -12 thru -6 of fileName
		my writeto(myLog, "point 1" & linefeed, text, true)
	else
		set baseName to text 1 thru -6 of fileName
		my writeto(myLog, "point 2" & linefeed, text, true)
	end if
else if fileExtension is missing value then
	set baseName to text -7 thru -1 of fileName
else
	error "wrong kind of file"
end if
my writeto(myLog, "baseName = " & baseName & linefeed, text, true)

set PDFPath to "mymountedVolume:"

tell application "MultiAd Creator Pro"
	activate
	--try
	# aFile is an alias so there is no need to explicitely define the class
	--open (aFile as alias)
	my writeto(myLog, "point 3" & linefeed, text, true)
	open aFile
	my writeto(myLog, "point 4" & linefeed, text, true)
	set theDoc to document 1
	my writeto(myLog, "point 5" & linefeed, text, true)
	set destFolder to PDFPath
	my writeto(myLog, "point 6" & linefeed, text, true)
	# Assuming that the filename match the original asker description,
	# you are trying to export into : "mymountedVolume:1234567:myfilename1234567.crtr.pdf"
	# Is it really what you want.
	my writeto(myLog, "point 7, want to save in :" & linefeed & (destFolder & baseName & ".pdf") & linefeed, text, true)
	display dialog "want to save in :" & linefeed & (destFolder & baseName & ".pdf")
	--tell application "MultiAd Creator Pro" # No need for that, you are already speaking to this application
	(*	
                        export PDF current spread of theDoc saving in (destFolder & baseName & ":" & fileName & ".pdf") print order user order export method composite output color space CMYK stand in resolution 300 black and white compression use zip color image compression use JPEG quality 100 with embed no base 14 fonts, use doc size, binary encoding and compress text and line art without print as spreads, crop marks, registration marks, color bars, document notes, plate information, text blocks only and presentation mode
						
					*)
	-- end tell
	my writeto(myLog, "point 8" & linefeed, text, true)
	close front document saving no
	my writeto(myLog, "point 9" & linefeed, text, true)
	(*
				on error e
					display dialog "an error occured " & e
				end try
				*)
end tell


#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

#=====

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 18 mai 2017 20:44:55

went with typeID

on open droppedItems
repeat with aFile in droppedItems
set {name:fileName, name extension:fileExtension, type identifier:typeID} to info for aFile
if typeID is “com.multiad.creator.crtr” then
if fileExtension is “” or fileExtension is missing value then
set baseName to text -7 thru -1 of fileName
else
set extLen to (length of fileExtension) + 1 --Add 1 for ‘.’.
set baseName to text (-7 - extLen) thru (-1 - extLen) of fileName
end if

Thanks for the feedback.

On my side I never use info for which is officially deprecated for years.
Apple may remove it some of these days.
This is why I would replace

  set {name:fileName, name extension:fileExtension, type identifier:typeID} to info for aFile

by

# set aFile to ((path to desktop as text) & "2x0.5_1234567") as alias

tell application "Finder"
	set {fileName, fileExtension} to {name of aFile, name extension of aFile}
end tell
# CAUTION, only Finder return the correct extension with a name like : "2x0.5_1234567"
tell application "System Events" to tell disk item (aFile as text)
	set typeID to type identifier
end tell

if typeID is "com.multiad.creator.crtr" then
	if fileExtension is in {"", missing value} then
		set baseName to text -7 thru -1 of fileName
	else
		tell (count fileExtension) to set baseName to text -(it + 8) thru -(it + 2) of fileName
	end if
	#.
end if

I choose to ask Finder for the fileName because System Events embedded slashes by underscores.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) vendredi 19 mai 2017 11:39:15

Because they might want to perform some action in addition to opening the item?

I thought it relevant to answer the first part of the OP’s question ” even if it isn’t directly related to the workflow he describes later.

-Chris

but something still isn’t right

it is not giving me the correct PDF file name??

for a file name of "In Memorium 2x0.5_1234567

the resulting PDF is “ium 2x0”?

the result should be “1234567.pdf”

my count is off or where i start my count??

set {name:fileName, name extension:fileExtension, type identifier:typeID} to info for aFile
if typeID is “com.multiad.creator.crtr” then
if fileExtension is “” or fileExtension is missing value then
set baseName to text -7 thru -1 of fileName
else
set extLen to (length of fileExtension) + 1 --Add 1 for ‘.’.
set baseName to text (-7 - extLen) thru (-1 - extLen) of fileName
end if

It’s ridiculous but when a file is named “2x0.5_1234567”
info for and System Events return : “5_1234567” when they are asked for name extension.
Only the Finder returns a correct value.
Alas, the Finder ignores type identifier.

I edited the script in message #19 to take care of that.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 23 mai 2017 15:18:11

it did work. but i am not sure why :slight_smile:

on open droppedItems
repeat with aFile in droppedItems
# set aFile to ((path to desktop as text) & “2x0.5_1234567”) as alias
tell application “Finder”
set {fileName, fileExtension} to {name of aFile, name extension of aFile}
end tell
# CAUTION, only Finder return the correct extension with a name like : “2x0.5_1234567”
tell application “System Events” to tell disk item (aFile as text)
set typeID to type identifier
end tell

	if typeID is "com.multiad.creator.crtr" then
		if fileExtension is in {"", missing value} then
			set baseName to text -7 thru -1 of fileName
		else
			tell (count fileExtension) to set baseName to text -(it + 8) thru -(it + 2) of fileName
		end if
		#.
	end if
	if typeID is "com.multiad.creator.crtr" then
		if fileExtension is "" or fileExtension is missing value then
			set baseName to text -7 thru -1 of fileName
		else
			set extLen to (length of fileExtension) + 1 --Add 1 for '.'.
			set baseName to text (-7 - extLen) thru (-1 - extLen) of fileName
		end if
		set PDFPath to "DisplayAds_HiRes:"
		
		tell application "MultiAd Creator Pro"
			activate
			try
				open (aFile as alias)
				set theDoc to document 1
				set destFolder to PDFPath
				
				
				export PDF current spread of theDoc saving in (destFolder & baseName & ".pdf") print order user order export method composite output color space CMYK stand in resolution 300 black and white compression use zip color image compression use JPEG quality 100 with embed no base 14 fonts, use doc size, binary encoding and compress text and line art without print as spreads, crop marks, registration marks, color bars, document notes, plate information, text blocks only and presentation mode
				
				
				-- end tell
				close front document saving no
			on error e
				display dialog "an error occured " & e
			end try
		end tell
		
	end if
end repeat

end open

Maybe this cleaned and commented version will help you to understand.

on open droppedItems
	# droppedItems is a list of aliases dropped on the folder icon
	repeat with aFile in droppedItems
			
		set aFile to contents of afile # ADDED

		# Trigger Finder to get the name without replacing slash(es) by underscore(s)
		# and the TRUE name extension. 5system Events and info for may return wrong values.
		tell application "Finder"
			set {fileName, fileExtension} to {name of aFile, name extension of aFile}
		end tell
		# Only info for and System Events return type identifier but info for is deprecated.
		tell application "System Events" to tell disk item (aFile as text)
			set typeID to type identifier
		end tell
		
		if typeID is "com.multiad.creator.crtr" then
			if fileExtension is in {"", missing value} then
				set baseName to text -7 thru -1 of fileName # Extract the 7 digits at end of the name
			else
				# Extract the 7 digits preceeding the name extension (and its dot)
				tell (count fileExtension) to set baseName to text -(it + 8) thru -(it + 2) of fileName
			end if
			# I removed some unneeded instructions
			-- set PDFPath to "DisplayAds_HiRes:" # What need to define it here ?

			tell application "MultiAd Creator Pro"
				activate
				try
					# No need to coerce aFile as alias because it's already an alias object !
					open aFile # EDITED
					set theDoc to document 1
					set destFolder to "DisplayAds_HiRes:" --PDFPath # define it here does a perfect job !
					
					
                    export PDF current spread of theDoc saving in (destFolder & baseName & ".pdf") print order user order export method composite output color space CMYK stand in resolution 300 black and white compression use zip color image compression use JPEG quality 100 with embed no base 14 fonts, use doc size, binary encoding and compress text and line art without print as spreads, crop marks, registration marks, color bars, document notes, plate information, text blocks only and presentation mode
                    
                    
					close front document saving no
				on error e
					display dialog "an error occured " & e
				end try
			end tell
			
		end if
	end repeat
end open

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 23 mai 2017 17:28:11

Thank you very very much Yvan! your were a big help!! Thank you.

I ran the script as you posted

and the script failed at:

open aFile

with “an error occurred Can’t handle that object type.”
“cancel” “ok”

i tried:

open (aFile as alias)

and it worked without issue?

i am just thankful that it is working :slight_smile:

It seems that I must apologize.

I tested the open instruction with droplets supposed to open PDF, text/Rtf, Pages, Numbers files
All of them didn’t required to use (aFile as alias).
I didn’t imagine that Multi Add Creator behave differently.

As I am curious I ask you to try to use the syntax:
open (contents of aFile)

I assume that it works as it does with :

set aFolder to (path to desktop as text) & "theSource:" # Edit to fit your needs.
tell application "Finder"
	set theFiles to (every file of folder aFolder) as alias list
	--> {alias "SSD 500:Users:??????????:Desktop:theSource:France_Garantie.pdf", alias "SSD 500:Users:??????????:Desktop:theSource:RAW-Power-Help-file.pdf"}
end tell
tell application "MultiAd Creator Pro" # ADDED
repeat with aFile in theFiles
	aFile
	-->item 1 of {alias "SSD 500:Users:??????????:Desktop:theSource:France_Garantie.pdf", alias "SSD 500:Users:??????????:Desktop:theSource:RAW-Power-Help-file.pdf"}
	--> item 2 of {alias "SSD 500:Users:??????????:Desktop:theSource:France_Garantie.pdf", alias "SSD 500:Users:??????????:Desktop:theSource:RAW-Power-Help-file.pdf"}
end repeat

repeat with aFile in theFiles
	contents of aFile
	--> alias "SSD 500:Users:??????????:Desktop:theSource:France_Garantie.pdf"
	--> alias "SSD 500:Users:??????????:Desktop:theSource:RAW-Power-Help-file.pdf"
end repeat

end tell # ADDED

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 23 mai 2017 20:40:53

how is this script applied?

on run the result is

error “Can’t make every file of «class cfol» "Macintosh HD:Users:me:Desktop:theSource:" of application "Finder" into type alias list.” number -1700 from every file of «class cfol» “Macintosh HD:Users:me:Desktop:theSource:” to «class alst»

The script is written to extract the contents of a folder named “theSource” on the Desktop.
Maybe it would have been useful to add the comment : # Edit to fit your needs.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 24 mai 2017 13:41:28

created a folder “theSource” on desktop

placed in two Creator test files:

“mytestfile 33333333”

“mytestfile3x3_6533333337”

ran script

the result
alias “Macintosh HD:Users:me:Desktop:theSource:mytestfile3x3_6533333337.crtr”

Thanks for the feedback.
As I guessed, in your main script, you may use the syntax : open (contents of aFile).

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 24 mai 2017 15:41:04

open (contents of aFile) would replace open (aFile as alias)…correct?

Exactly.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 24 mai 2017 17:01:01

will you get upset if i tell you that the script gives the error as i reported earlier??

tell application “MultiAd Creator Pro”
activate
try
open (contents of aFile)
set theDoc to document 1
set destFolder to “DisplayAds_HiRes:”

gives the error

“an error occurred Can’t handle that object type.”

I added two instructions to the script in message #26.

May you run this new version and report what it returns ?

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 24 mai 2017 18:29:07