Droplet script - Open File in Native Application

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

theSource folder contains the following 3 files
mytestfile_33333334.crtr
mytestfile3x3_6533333337
mytestfile 33333333

running the script
resulted in

alias “Macintosh HD:Users:me:Desktop:theSource:mytestfile_33333334.crtr”

Thanks.

You may now try the main script edited in message #24.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 25 mai 2017 10:07:26

Success!!! The script functioned without error or modification!

I am very grateful and learned very much from the experience.

Thank you again for all your time and consideration!! :slight_smile:

Rick