Droplet script - Open File in Native Application

I am very new to Applescript (aka I’ve crash-course taught myself through google this past weekend.)

What I’m trying to do is write a script that will open whatever file that is dropped on it in it’s native application. (ie: .indd file would launch InDesign, .txt file would open in TextEdit) I’d even be willing to settle if the file would always open in InDesign.

I’ve tried the manipulate the following code to adapt to my desired outcome:

on open theDrop
	display dialog "Hey! You dropped " & theDrop's length & " file(s) on me!"
end open

I’ve had no luck whatsoever.

The script I’d like to write overall is this:
¢ Drop file onto droplet.
¢ Open file in InDesign.
¢ Export file as PDF (with a default preset?)
¢ Save file.
¢ Close document.
¢ Minimize InDesign.

Thanks for any help. I feel like it’s probably really easy I just can’t find the answer & I’ve searched for hours.

Cheers!
-Tia

ps. I’ve referenced this post for the droplet part: http://macscripter.net/viewtopic.php?id=24772

Model: Macbook Pro
AppleScript: 2.2.1
Browser: Safari 533.22.3
Operating System: Mac OS X (10.5)

Hi,

try this, change the literal string in the first line to the name of your preferred preset


property PDFPresetName : "[NameOfPreset]"

on open droppedItems
	repeat with aFile in droppedItems
		set {name:fileName, name extension:fileExtension} to info for aFile
		if fileExtension is "txt" then
			tell application "TextEdit" to open aFile
		else if fileExtension is "indd" then
			set baseName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
			tell application "Finder" to set parentDirectory to (container of aFile) as text
			set PDFFilePath to parentDirectory & baseName & ".pdf"
			tell application "Adobe InDesign CS3"
				try
					open aFile
					set PDFPreset to PDF export preset PDFPresetName
					export front document format PDF type using PDFPreset to PDFFilePath without showing options
					close front document saving no
				on error e
					display dialog "an error occured " & e
				end try
			end tell
		end if
	end repeat
end open

THANK YOU Stefank! That worked great!

I modified the script to use the network volume on my desktop.

set PDFFilePath to parentDirectory & baseName & ".pdf"

i changed parentDirectory to “myVolume:”

and it worked thank you again!!! perfect!!!

R

I do have one more modification that i am not sure how to handle:

i need the resulting PDF file name to be the last 7 characters of the filename.pdf

for example: if the original file name is myfilename1234567.indd

the PDF result should be 1234567.pdf

any thought on how to make that work???

Rick


i did some tinkering and came up with

set baseName to text -12 thru ((get offset of “.” & fileExtension in fileName) - 1) of fileName

it works but does it make sense?? lol

Yes, it does

Your code does the job but only if the extension is 4 characters long.
If you are sure of that, it would be simpler to code :
set baseName to text -12 thru -6 of fileName

If you want a more generic code, it would be better to use :


set off7 to get offset of "." & fileExtension in fileName
set baseName to text (off7 - 7) thru (off7 - 1) of fileName

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mercredi 17 mai 2017 16:12:29

Hi.

Since that piece of code is only executed if the file extension is “indd” (four characters), and you know the extension comes at the end of the name and is preceded by a dot (one character), you could hard-code the extraction of the seven-character substring before than more simply like this:

set baseName to text -12 thru -6 of fileName

If the code were for a number of possible extensions of different lengths, you might use:

set dotOffset to (count fileName) - (count fileExtension)
set baseName to text (dotOffset - 7) thru (dotOffset - 1) of fileName

Or, more cryptically:

tell ((count fileExtension) + 2) to set baseName to text -(6 + it) thru -it of fileName

Hey TIaNichole,

As you’ve seen it’s easy enough to code for various file types.

On the other hand if you literally want an item to open in its native application, you can use the Finder.


on open (ddInput)
	tell application "Finder"
		repeat with theItem in ddInput
			open theItem
		end repeat
	end tell
end open

-Chris

True, but if that’s really the desired functionality, then I don’t know why anyone would want a droplet rather than just hitting [command] + “o” once the files are selected.

at the risk of taking this thread on a tangent, i was able to get the Stefank script working for InDesign (thank you to all who participated!)

but then i thought i could reverse engineer the script to work with another program and failed miserably. The program is Multi-Ad Creator Pro and i am guessing none of you have it lol

one major difference is that in the Creator script I call out the PDF specs instead of using a Preset…but that is not the issue. For some reason the script doesn’t recognize the existence of a file extension and beyond that i I can’t even get the script to Open the file in Creator.

here is what i have:

on open droppedItems
repeat with aFile in droppedItems
set {name:fileName, name extension:fileExtension} to info for aFile
if fileExtension is “crtr” then
set baseName to text -12 thru -6 of fileName
set PDFPath to “mymountedVolume:”
tell application “MultiAd Creator Pro”
try
open (aFile as alias)
set theDoc to document 1

				set destFolder to PDFPath
				
				tell application "MultiAd Creator Pro"
					
					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
				
				
				
				close front document saving no
			on error e
				display dialog "an error occured " & e
			end try
		end tell
	end if
end repeat

end open

@rick_260
I assume that you really dropped a file whose extension is “crtr”.
Are you sure that your filenames are long enough to accept the instruction : set baseName to text -12 thru -6 of fileName
The original asker use filenames like “myfilename1234567.indd” and he wished to extract the component “1234567”.

May you try to run this modified version:

on open droppedItems
	repeat with aFile in droppedItems
		set {name:fileName, name extension:fileExtension} to info for aFile
		
		if fileExtension = "crtr" then
			if (count fileName) > 12 then
				set baseName to text -12 thru -6 of fileName
			else
				set baseName to text 1 thru -6 of fileName
			end if
			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)
					open aFile
					set theDoc to document 1
					
					set destFolder to PDFPath
					# 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.
					display dialog "want to save in :" & linefeed & (destFolder & baseName & ":" & fileName & ".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
					close front document saving no
				on error e
					display dialog "an error occured " & e
				end try
			end tell
			
		end if
	end repeat
end open

I disabled the export instruction for several reasons :
1 - as I don’t own the app the script doesn’t compile here.
2 - I’m not sure that the built pathname is the correct one
3 - I’m not sure that the app accepts a string to define the pathname of the exported item
A dialog will show you the built pathname.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 18 mai 2017 16:03:25

i will give the script a try and post the results thank you!

yes the files are “crtr”

and i need to capture the last 7 characters of the file name.pdf

(ex: my_file 1234567 would result in a PDF 1234567.pdf)

It’s not what your code does.

At this time you are building :
“mymountedVolume:1234567:myfilename1234567.crtr.pdf”
To achieve your goal you would have to replace :
(destFolder & baseName & “:” & fileName & “.pdf”)
by
(destFolder & baseName & “.pdf”)

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 18 mai 2017 16:16:34

debugged

using “crtr” file however the files are saved without the .crtr extension

created a test file with the .crtr extension

and then ran into a problem

*the problem appears to be in the lines
try
open aFile

  • the fix was
    open (aFile as alias)

on open droppedItems
repeat with aFile in droppedItems
set {name:fileName, name extension:fileExtension} to info for aFile
if fileExtension = “crtr” then
if (count fileName) > 12 then
set baseName to text -12 thru -6 of fileName
else
set baseName to text 1 thru -6 of fileName
end if
set PDFPath to “PDFProofs:”

		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

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