Page 1 of 1

Make default app depend on location of file?

Posted: 15 Jun 2016 22:16
by kunkel321
If a PDF is in my /Downloads/ folder, I usually want to open it with Sumatra PDF. If it's a PDF in any other folder though, I usually want to open it with Acrobat. What would be the best way to automate this? Best case scenario would be for this behavior to occur when I double-click a PDF. But maybe this is controlled by Windows (?) A scripted toolbar button, or keyboard command would maybe be okay too. (I do already have both apps set in my Portable App Associations toolbar command). Thoughts?

Re: Make default app depend on location of file?

Posted: 15 Jun 2016 22:29
by highend
A PFA would do it with a minimal script that checks for the folder, like:

Code: Select all

pdf>::if (<pfaitem> LikeI "*\Downloads\*") { run """<path to sumatra.exe>"" ""<pfaitem>"""; } else { run """<path to adobe acrobat.exe>"" ""<pfaitem>"""; }

Re: Make default app depend on location of file?

Posted: 16 Jun 2016 14:29
by TheQwerty
Personally, it comes down to the number and type of special cases you have.

For the scenario mentioned I'd use two PFAs:

Code: Select all

*\Downloads\*.pdf>sumatra.exe
pdf>acrobat.exe
One advantage to this approach is that in your Downloads folder the open with menu will show both entries allowing you to temporarily use acrobat if desired. Unfortunately, outside of that folder you'll only see the acrobat option unless you also add a third entry:

Code: Select all

*\Downloads\*.pdf>sumatra.exe
pdf>acrobat.exe
pdf>sumatra.exe
Another advantage is it means you don't have to give consideration to how your script works when called with multiple items - which can be a bit confusing.


However, this does not scale well when there are more special cases. For instance if you wanted to apply this same behavior to epub files (ignoring that acrobat cannot handle them). Then you'd need:

Code: Select all

*\Downloads\*.pdf;*\Downloads\*.epub>sumatra.exe
pdf;epub>acrobat.exe
At present there's no good way to consolidate the patterns for sumatra - something similar to Color Filter's pre-filters or regex support would help greatly.


My suggestion is start with multiple PFAs and when the number, length, or complexity annoys you and you have the time convert to a script as highend suggested.

But please put that script in a file and load it from your PFA! :P

Present-me is a huge fan of one-liners but future-me always wants to kill past-me for the nightmares they leave behind! :twisted:

Re: Make default app depend on location of file?

Posted: 19 Jun 2016 22:27
by kunkel321
Thanks for the tips here Guys!

At first I tried regurgitating these into an .xys that I could call from a toolbar button. That failed miserably. :roll:
Then I figured I'd start-over by just pasting Highend's suggestion right into the list of PFA definitions (adding the paths of course). (as a oneliner -- :wink: ) Low and behold it works as expected, but (unexpectedly to me) it also works if I just double-click the PDF file, not even using the PFA list that's on my toolbar! Hooray. :tup:

Interesting note: Neither pfaitem nor LikeI appear to be in the xyplorer help file....

Re: Make default app depend on location of file?

Posted: 19 Jun 2016 22:53
by highend
but (unexpectedly to me) it also works if I just double-click the PDF file
That's because you have:
Open Items by First "Open With..." Match
ticked (right click on the PFA icon for the context menu)
Neither pfaitem nor LikeI appear to be in the xyplorer help file....
Ehem, you should better read it :D

"<pfaitem>" is in Advanced Topics - Portable File Associations (The special variable <pfaitem>)
and
"LikeI" is in Advanced Topics - Scripting - Comparisons...

Re: Make default app depend on location of file?

Posted: 20 Jun 2016 16:46
by kunkel321
highend wrote:
but (unexpectedly to me) it also works if I just double-click the PDF file
That's because you have:
Open Items by First "Open With..." Match
ticked (right click on the PFA icon for the context menu)
Indeed I do!
highend wrote:[...]"<pfaitem>" is in Advanced Topics - Portable File Associations (The special variable <pfaitem>)
and
"LikeI" is in Advanced Topics - Scripting - Comparisons...
Strange... I could've sworn I had used the "Search" feature of the compiled help, and did not find either... I just tried it again though, and was able to find each... I guess I'm just losing it. :oops: