Author Topic: Mac: Starting PM from terminal and open a folder as contact sheet  (Read 4612 times)

Offline Sven

  • Uber Member
  • ******
  • Posts: 1022
    • View Profile
Hi @ all!

I receive on a regular basis one or more ZIP-files and I need to view the contained images.

I have set up a terminal command that extracts the content of the zip(s) into a (never changing) folder.
I know how to open up PM using the terminal.
After extracting all the ZIP's it would be great to have PM opened up automatically and displaying the content of the extract folder.

Question: Is there any terminal parameter to open a folder right after launching PM?
I know that I can set the default behavior in the prefs of PM but if I start up PM the "usual" way I want to start it with no contact sheet opened up.

I am thinking of something like "open '/Applications/Photo Mechanic 5.app' /Volumes/Path/to/image/folder"

Thanks
Sven
Changed from behind the cam to one who buys images as I started to run. No cam or lens left.

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 23249
    • View Profile
    • Camera Bits, Inc.
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #1 on: November 11, 2012, 10:00:06 AM »
Sven,

I receive on a regular basis one or more ZIP-files and I need to view the contained images.

I have set up a terminal command that extracts the content of the zip(s) into a (never changing) folder.
I know how to open up PM using the terminal.
After extracting all the ZIP's it would be great to have PM opened up automatically and displaying the content of the extract folder.

Question: Is there any terminal parameter to open a folder right after launching PM?
I know that I can set the default behavior in the prefs of PM but if I start up PM the "usual" way I want to start it with no contact sheet opened up.

I am thinking of something like "open '/Applications/Photo Mechanic 5.app' /Volumes/Path/to/image/folder"

Photo Mechanic on the Mac OS X platform does not process command line parameters.

-Kirk

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2531
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #2 on: November 11, 2012, 10:45:15 AM »
Kirk, Sven, perhaps there is a way to interact with/fire up the browse with pm service instead?

Cheers,
Hayo
Hayo Baan - Photography
Web: www.hayobaan.nl

Offline Sven

  • Uber Member
  • ******
  • Posts: 1022
    • View Profile
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #3 on: November 11, 2012, 09:59:47 PM »
Hayo,

that's an option. As this is a binary I cannot find out how this is called...
That's the reason I asked here.

Sven
Changed from behind the cam to one who buys images as I started to run. No cam or lens left.

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 23249
    • View Profile
    • Camera Bits, Inc.
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #4 on: November 12, 2012, 06:51:52 AM »
Perhaps there is a way to interact with/fire up the browse with pm service instead?

There won't be any way to use the service to do the work, but one could use a fairly simple AppleScript to do it.  It would be something like:

tell application Photo Mechanic
  open folder with posix path "your path"
end tell

I don't know if that sample would work as written (can't remember the file/folder semantics off hand.)

-Kirk

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2531
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #5 on: November 12, 2012, 07:34:44 AM »
Perhaps there is a way to interact with/fire up the browse with pm service instead?

There won't be any way to use the service to do the work, but one could use a fairly simple AppleScript to do it.  It would be something like:

tell application Photo Mechanic
  open folder with posix path "your path"
end tell

I don't know if that sample would work as written (can't remember the file/folder semantics off hand.)

Played around a bit with applescript (haven't really used it before, so this was a good exercise) and remembered that you can use osascript to run scripts from the command line. Here's a oneliner that would work in Sven's terminal script:

osascript -e "tell application \"Photo Mechanic 5\"" -e "open \"full path to folder\"" -e "end tell"

Have fun,
Hayo
Hayo Baan - Photography
Web: www.hayobaan.nl

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 23249
    • View Profile
    • Camera Bits, Inc.
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #6 on: November 12, 2012, 08:06:55 AM »
Hayo,

Perhaps there is a way to interact with/fire up the browse with pm service instead?

There won't be any way to use the service to do the work, but one could use a fairly simple AppleScript to do it.  It would be something like:

tell application Photo Mechanic
  open folder with posix path "your path"
end tell

I don't know if that sample would work as written (can't remember the file/folder semantics off hand.)

Played around a bit with applescript (haven't really used it before, so this was a good exercise) and remembered that you can use osascript to run scripts from the command line. Here's a oneliner that would work in Sven's terminal script:

osascript -e "tell application \"Photo Mechanic 5\"" -e "open \"full path to folder\"" -e "end tell"

That will likely work as long as the path doesn't have any non-ascii characters.  If the path has non-ascii characters the 'posix path' keywords will need to be added and accented characters will have to be represented with escaped octal byte codes.  Luckily the Terminal's path completion system will do this for you--just press Tab while you're entering the path and the Terminal will complete it for you (if there are other folders that have similar names then the Terminal will beep at you until you either enter enough characters to differentiate your chosen path or press Tab again which will cause the Terminal to show you the matches).

-Kirk

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2531
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #7 on: November 12, 2012, 11:57:56 AM »
That will likely work as long as the path doesn't have any non-ascii characters.  If the path has non-ascii characters the 'posix path' keywords will need to be added and accented characters will have to be represented with escaped octal byte codes.  Luckily the Terminal's path completion system will do this for you--just press Tab while you're entering the path and the Terminal will complete it for you (if there are other folders that have similar names then the Terminal will beep at you until you either enter enough characters to differentiate your chosen path or press Tab again which will cause the Terminal to show you the matches).

Hmm, I just tested this on a directory with an ndash (–) and one with an o-umlaut (ö) and ampersand (&) in the name and it just worked…
Note, however that I work in a UTF-8 enabled shell (but to my knowledge that's the default on a Mac these days) but as this also works just fine in the applescript editor, I think non-ascii characters are actually not a problem (any longer?) — or am I doing something special here?

Instead of using multiple -e arguments to osascript you can also run it with a multi-line “script”:
osascript -e 'tell application "Photo Mechanic 5"
open "<full path to folder>"
end tell'

(the use of the single quote saved me from having to escape the double quotes, use what you like in your script)
« Last Edit: November 12, 2012, 12:01:27 PM by Hayo Baan »
Hayo Baan - Photography
Web: www.hayobaan.nl

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 23249
    • View Profile
    • Camera Bits, Inc.
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #8 on: November 12, 2012, 01:51:50 PM »
Hayo,

That will likely work as long as the path doesn't have any non-ascii characters.  If the path has non-ascii characters the 'posix path' keywords will need to be added and accented characters will have to be represented with escaped octal byte codes.  Luckily the Terminal's path completion system will do this for you--just press Tab while you're entering the path and the Terminal will complete it for you (if there are other folders that have similar names then the Terminal will beep at you until you either enter enough characters to differentiate your chosen path or press Tab again which will cause the Terminal to show you the matches).

Hmm, I just tested this on a directory with an ndash (–) and one with an o-umlaut (ö) and ampersand (&) in the name and it just worked…
Note, however that I work in a UTF-8 enabled shell (but to my knowledge that's the default on a Mac these days) but as this also works just fine in the applescript editor, I think non-ascii characters are actually not a problem (any longer?) — or am I doing something special here?

Instead of using multiple -e arguments to osascript you can also run it with a multi-line “script”:
osascript -e 'tell application "Photo Mechanic 5"
open "<full path to folder>"
end tell'

(the use of the single quote saved me from having to escape the double quotes, use what you like in your script)

It's certainly possible that things have improved in this area.  YMMV.

-Kirk

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2531
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #9 on: November 16, 2012, 12:05:54 AM »
Hah, I found the ability to be able to open a folder from the command-line so handy, I decided to write a little script to open the folders provided as arguments. It's very basic and only checks to see if the provided arguments are indeed files/folders.

Thank you Sven for bringing this up, now I have a new handy feature available on the command-line!

Enjoy  :)

#!/usr/bin/perl

use Cwd qw(abs_path);

for my $folder (@ARGV) {
  if (-d $folder || -f $folder) {
    my $script ='tell application "Photo Mechanic 5"
open "' . abs_path($folder) . '"
end tell';
    system("osascript -e '$script' 2>&1 > /dev/null");
  } else {
    print STDERR "Error: Not a file/folder $folder\n";
  }
}
« Last Edit: November 16, 2012, 12:08:04 AM by Hayo Baan »
Hayo Baan - Photography
Web: www.hayobaan.nl

Offline Sven

  • Uber Member
  • ******
  • Posts: 1022
    • View Profile
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #10 on: November 17, 2012, 12:26:45 AM »
Kirk, Hayo!

Sorry for not responding in time... I have been on a business trip for the last days...

I tried to use Hayo's latest coding but it gave me an error...
But the one before works only if PM is already up and running.

Adding one more line in front works well :)

Code: [Select]
open '/Applications/Photo Mechanic 5.app'
osascript -e 'tell application "Photo Mechanic 5"
open "<full path to folder>"
end tell'

@Hayo: Many thanks!
Changed from behind the cam to one who buys images as I started to run. No cam or lens left.

Offline Hayo Baan

  • Uber Member
  • ******
  • Posts: 2531
  • Professional Photographer & Software Developer
    • View Profile
    • Hayo Baan - Photography
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #11 on: November 18, 2012, 06:33:20 AM »
I tried to use Hayo's latest coding but it gave me an error...
Hmm, what kind of error? It's the exact script I'm using currently and works great for me (did you save the script as a file in a  folder in your path and did you make it executable?)

But the one before works only if PM is already up and running.
Yeah, I found this out myself too (quite logical too; how can applescript talk to an application that's not even running?), the additional line works well and have added it to my script:
Code: [Select]
#!/usr/bin/perl

use Cwd qw(abs_path);

system("open -a 'Photo Mechanic 5'");
for my $folder (@ARGV) {
  if (-d $folder || -f $folder) {
    my $script ='tell application "Photo Mechanic 5"
open "' . abs_path($folder) . '"
end tell';
    system("osascript -e '$script' 2>&1 > /dev/null");
  } else {
    print STDERR "Error: Not a file/folder $folder\n";
  }
}

@Hayo: Many thanks!
You're most welcome, besides, you gave me the idea and coupled with Kirk's input I now have a nice feature  8)
Hayo Baan - Photography
Web: www.hayobaan.nl

Offline Sven

  • Uber Member
  • ******
  • Posts: 1022
    • View Profile
Re: Mac: Starting PM from terminal and open a folder as contact sheet
« Reply #12 on: November 18, 2012, 07:11:32 AM »
Hayo,

I tried to use Hayo's latest coding but it gave me an error...
Hmm, what kind of error? It's the exact script I'm using currently and works great for me (did you save the script as a file in a  folder in your path and did you make it executable?)

I had a few copy / move stuff and ZIP-extracts before. When using
Code: [Select]
#!/usr/bin/perl it failed.
Now I added the few lines I posted into my script and all is well :D

Sven
Changed from behind the cam to one who buys images as I started to run. No cam or lens left.