Quirky path to me problem

I found a post in 2011 that helped me figure out a solution a problem using (path to me as string). I have a a 1 page pdf with a password of “123.” I wrote an applescript app with MyPDF.pdf sitting inside Package Contents. I then wrote a 2nd script - MyScript - that launches MyApp. When MyPDF opens, code in tMyScript inputs 123 and opens MyPDF. It works fine. In MyScript, System Events keystrokes 123.

I’ve spent hours trying to make this happen in one script - but can’t. Is that possible? When MyApp opens the pdf - sitting inside the Contents directory - MyApp comes to a dead halt - it won’t continue with further commands which is why I had to divide it into two applescripts.

My goal is to place the entire thing in one app. Thank you much for any help.

PS
In an apple forum, I read an explanation about how dbl clicking on an AS app vs running it from inside the script are 2 different animals, Finder vs ScriptEditor - when path to me is used. I think this is what I’m struggling with but I’m not sure. That discussion is here:

https://discussions.apple.com/thread/1049296?tstart=0

Thanks again!


set keyvalue to "123" & return
delay 1
tell application "Finder"
	open file "MyApp.app" of folder "Desktop" of home ¬
		
	delay 1
	tell application "Adobe Reader"
		activate
	end tell
	delay 1
	tell application "System Events" to keystroke keyvalue
end tell

tell application id "com.adobe.Reader" to launch
delay 1
tell application id "com.adobe.Reader"
	open ((path to me as string) & "MyPDF.pdf") as alias
	activate
end tell

Hi.

In an applet, ‘path to me’ returns the path to the applet package, so if your PDF’s in the Contents folder, you need to include that in the path too.

((path to me as text) & "Contents:MyPDF.pdf") as alias

If it was in the Resources folder, you could use either .

((path to me as text) & "Contents:Resources:MyPDF.pdf") as alias

. or .

(path to resource "MyPDF.pdf") -- no coercions required.

As a precaution, it’s a good idea not to use these in an application ‘tell’ block, so:

tell application id "com.adobe.Reader" to launch
delay 1
set pdfFile to ((path to me as text) & "Contents:MyPDF.pdf") as alias
tell application id "com.adobe.Reader"
	open pdfFile
	activate
end tell

I’ve not been able to try the password business.

This is first time I saw this reply. Thanks!

I’ll use what you write above. Til now, I had the pdf sitting inside the AS app, at the same level as the Contents folder and was using this:


tell application id "com.adobe.Reader" to launch
delay 1
tell application id "com.adobe.Reader"
	open ((path to me as string) & "My.pdf") as alias
	activate
end tell

Re keystroking. I think I’ve got that part licked but there’s one problem. In the script, I use set keyvalue to “MyPassword” and then later tell application “System Events” to keystroke keyvalue. The delays have to be right but it’s good-enuf. Something I can’t figure out, tho: If I open MY.pdf with application “Adobe Reader” or application id ".com.adobe-blah-blah, System Events won’t keystroke the keyvalue. Nothing keystrokes. The script dies at that point. But if I open the pdf with app “Finder,” System Events keystrokes.

This is a problem because, if I was able to open the pdf with Reader, I can easily activate the PW dialogue box when it comes up. (I tried making the PW window foremost - but nothing brings it foremost and available for the PW keystroke [that I can figure out] - unless - if immediately before - 1, I use Finder to open the pdf and then 2, activate or again launch Reader. (I’m sure there are other workarounds I don’t know about, to make the PW dialog box activate). I don’t understand why this happens and it makes it cumbersome. …any feedback would be really 'preciated. Thanks again…!

What if you carefully code :

tell application "System Events" to tell process "Adobe Reader" # check the real name of the process
set frontmost to true
keystroke keyvalue
end tell

CAUTION, some not too old versions of macOS fail to execute keystroke keyvalue.
When we use them we must use

tell application "System Events" to tell process "Adobe Reader" # check the real name of the process
set frontmost to true
repeat with aChar in keyvalue
keystroke achar
delay .02 # according to your machine a greater delay may be required
end repeat
end tell

I reported the problem under 10.12.2 and some of my messages prove that the problem no longer stroke under 10.12.4.

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 1 juin 2017 18:59:50

Nigel, Just re-read your post and got what you’re saying. Your main points, if I got it right, is to be specific re the paths. I wasn’t doing this before and this may be why problems came up, like the script would halt and not do anything further. And rather than use “tell app…” directly with path to me, first set the path to me as Mypdf (or whatever) and then do the “tell block” business. Thank you…Peter

Yvan, I tried you suggestion - been working on it since you posted - but it doesn’t work (on my 10.10.5). I even tried the pid number - but if I use reader to open the pdf, the PW dialog box remains greyed out and inactive.

It’s strange because all I have to do is this:


tell application "Adobe Reader"
activate
	open My.pdf

and the PW box is active and ready to take a keystroke - but then, system events won’t keystroke (shrug). Using Finder to open the pdf - then “re” activating reader is a work around. There are other bigger obstacles I have to overcome besides this one, but … 'preciate your help. Peter