Jump to content

Script Filter question handling PDF


Recommended Posts

Hi

 

I have a PDF and I would like to use Alfred to let me select which chapter or page I want to open the PDF to.

 

I am using a Script Filter and have two issues:

 

1. When I press Enter on the search result it seems to do nothing. I have to use the right arrow and then hit Enter when I see 'Open'

2. Im not sure of the arguments to get it to open to a certain page.

 

Here is the workflow

 

 

cat << EOB
{"items": [



	{
		"uid": "image",
		"type": "file",
		"title": "Chapter 4",
		"subtitle": "Reinforcement Learning Page 20",
		"arg": "~/Dropbox/Fire.pdf",
		"autocomplete": "abc",
		"icon": {
			"type": "filetype",
			"path": "public.jpeg"
		}
	},	


	{
		"uid": "image",
		"type": "file",
		"title": "Chapter 6",
		"subtitle": "Reinforcement Learning Page 60",
		"arg": "~/Dropbox/Fire.pdf",
		"autocomplete": "xyz",
		"icon": {
			"type": "filetype",
			"path": "public.jpeg"
		}
	},	



	{
		"uid": "image",
		"type": "file",
		"title": "Chapter 10",
		"subtitle": "Reinforcement Learning Page 88",
		"arg": "~/Dropbox/Fire.pdf",
		"autocomplete": "asd",
		"icon": {
			"type": "filetype",
			"path": "public.jpeg"
		}
	},	




]}
EOB

 

 

I know I can open to a certain page (eg page 90) using AppleScript:

tell application "Preview"
    activate
    
    set theFile to POSIX file "~/Dropbox/Fire.pdf"
    open theFile
    
    tell application "System Events"
        keystroke "g" using {command down, option down}
        delay 0.5 
        keystroke "90"
        keystroke return
    end tell

end tell

 

 

But not sure how to do it in my workflow

 

I appreciate any guidance! 

 

Thanks

Edited by RandmTask
Link to comment

One way to do it using AppleScript would be to:

  • Use a Keyword with the argument as the page number you want.
  • Link the Keyword to your Eun Script AppleScript, set theQuery to item 1 of arg and, after the delay command, keystroke theQuery.

I've tried that and it works for me. No doubt there are more elegant ways of doing it but that is certainly workable.

 

Stephen

Link to comment

Thanks Stephen

 

I've tried both a simple keyword with an argument eg 'keyword 300' and a list filter to test it passing to the Run Script and it doesn't seem to work:

 

I updated the code in a 'Run Script' module to:

set theQuery to item 1 of arg

tell application "Preview"
    activate
    
    set theFile to POSIX file "~/Downloads/Fire.pdf"
    open theFile
    
    tell application "System Events"
        keystroke "g" using {command down, option down}
        delay 0.5
        keystroke theQuery
        keystroke return
    end tell

end tell

 

Link to comment

From your screenshot it appears that you're not leaving a space between the keyword and the number. The way it should work is that you type the keyword, then <space> then the number of the page you want. The script you mentioned in this post should then work.

 

Edit: In the keyword input don't include the number: simply the "test” keyword. When you run the workflow by typing the keyword then press <space> and input the number of the page you want.

 

Stephen

Edited by Stephen_C
Clarification
Link to comment

Hi Stephen

 

Sorry for the confusion. The 7 in test7 was not the page number, I was only trying to align with your use of the word test but had already used test for something else.

 

So, lets say instead of test7 the keyword is openpdf and I wanted to open page 300:

openpdf 300

 

The original script works:

tell application "Preview"
    activate
    
    set theFile to POSIX file "~/Downloads/Fire.pdf"
    open theFile
    
    tell application "System Events"
        keystroke "g" using {command down, option down}
        delay 0.5
        keystroke "300"
        keystroke return
    end tell

end tell

 


 

Updated code doesn't:

set theQuery to item 1 of arg

tell application "Preview"
    activate
    
    set theFile to POSIX file "~/Downloads/Fire.pdf"
    open theFile
    
    tell application "System Events"
        keystroke "g" using {command down, option down}
        delay 0.5
        keystroke theQuery
        keystroke return
    end tell

end tell

 

 

This line seems to be the issue

 

set theQuery to item 1 of arg

 

As even if I keep keystroke "300" in the script it stops working

 

 

 

Link to comment

Part of your script seems to be missing something crucial. The complete script should look like this:

on run argv
  set theQuery to item 1 of argv
tell application "Preview"
    activate
    
    set theFile to POSIX file "~/Downloads/Fire.pdf"
    open theFile
    
    tell application "System Events"
        keystroke "g" using {command down, option down}
        delay 0.5 
        keystroke theQuery
        keystroke return
    end tell

end tell
end run

Note carefully the first and last lines—and also ensure the Run Script action is set, at the top, with input as argv.

 

Edit: When you use the Run Script action set to AppleScript you'll find those missing lines are inserted for you. You usually put your script between them—at least if you are importing a variable (as you are: the number of pages).

 

Stephen

Edited by Stephen_C
Further explanation
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...