Applescript to pass Path to Save as Dialog Box for PDF

Follow up to an earlier post. I have written a script that opens an email message and then saves it as a PDF. It seems to be working but what I would like to do is chose the path and pass that to the dialog box but cannot work out how to do so. this is my script so far.


global FlName

tell application "Mail"
	set theMsg to selection
	open selection
	set Dialogresult to the button returned of (display dialog "Process this email and Print as PDF" buttons {"Yes", "No"} default button "Yes")
	if Dialogresult is "Yes" then
		repeat with selectMsg in theMsg
			tell selectMsg
				--set background color to red --Show message processed
				set FlName to subject
				set check to count every word of FlName
				--display dialog check
				set FlName to (my FixFileName(FlName)) --strip bad characters
			end tell
		end repeat
		set process_name to "Mail"
		activate application process_name
		tell application "System Events"
			tell process process_name
				--display dialog "proposed File Name" & return & FlName
				keystroke "p" using command down
				delay 2
				set PDFref to sheet 1 of window 1
				click menu button "PDF" of PDFref
				click menu item "Save as PDF." of menu 1 of menu button "PDF" of PDFref
				tell application "Mail"
					display dialog "Proposed Name " & my FlName default answer FlName & "change in the Box if Not OK"
					set FlName to text returned of result
				end tell
				keystroke FlName
			end tell
		end tell
	else
		set Dialogresult to the button returned of (display dialog "Close this eMail" buttons {"Yes", "no"} default button "No")
		if Dialogresult is "Yes" then
			close window 1
		end if
	end if
end tell

on FixFileName(str) --Deletes characters that cannot be used in file names
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
end FixFileName

Any suggestions appreciated
Thanks

Peter

It’s difficult to choose the path in a save dialog box. One idea is to save it to a known location, then move the file after. So when a dialog appears you can keystroke command-d which is a shortcut to make the dialog box go to the desktop. Then save the file and you know it’s on the desktop. Then move the desktop file.

Hi,

in a save dialog box you can change the destination folder by pressing ⇧⌘G and entering the (POSIX) path into the text field of the appearing sheet

I didn’t know that could be done… very nice.

I f I have already selected the path then created the file name and concatenated the two is there a way I can invoke ⇑⌘G with applescript assigning the variable I have previously created.

Thanks for your help

Peter

Hi MitchBVI,

I am not aware of your apps but this should do the trick in Safari:


activate application "Safari"
tell application "System Events" to tell application process "Safari" to click menu item "Save as." of menu "File" of menu bar 1

tell application "System Events" to tell application process "Safari" to keystroke "G" using {command down, shift down}
set RemRecPath to (path to library folder) as string
set RemShowPath to my substitute(RemRecPath, ":", "/")


tell application "System Events" to tell application process "Safari" to keystroke "/"
repeat with h in RemShowPath
	tell application "System Events" to tell application process "Safari" to keystroke h
end repeat
tell application "System Events" to tell application process "Safari" to keystroke return

on substitute(theText, toReplace, newText)
	set AppleScript's text item delimiters to the toReplace
	set the allTheText to every text item of theText
	set AppleScript's text item delimiters to the newText
	set theText to the allTheText as string
	set AppleScript's text item delimiters to ""
	return theText
end substitute

Hi,

this is the whole GUI scripting part in Mail with controlled delays


set destinationFolder to (choose folder)
activate application "Mail"
tell application "System Events"
	tell process "Mail"
		keystroke "p" using command down
		repeat until exists sheet 1 of window 1
			delay 0.2
		end repeat
		tell sheet 1 of window 1
			click menu button "PDF"
			click menu item "Save as PDF." of menu 1 of menu button "PDF"
		end tell
		repeat until exists window "Save"
			delay 0.2
		end repeat
		tell window "Save"
			keystroke "g" using {command down, shift down}
			repeat until exists sheet 1
				delay 0.2
			end repeat
			tell sheet 1
				set value of text field 1 to POSIX path of destinationFolder
				click button "Go"
			end tell
			repeat while exists sheet 1
				delay 0.2
			end repeat
			click button "Save"
		end tell
	end tell
end tell

:):slight_smile:
Stefan

That works thank you very much all I have to do now is figure out what to do with attachments.

Peter