Automate Creation of Wordpress Post from Smugmug Photo Gallery

I post a lot of sports photos to galleries in Smugmug, creating a new gallery for each game. I then create a Wordpress post using a Smugmug plug-in. It pulls all the photos from the gallery and creates a series of thumbnails. Photos continue to be hosted on Smugmug, saving me bandwidth.

The Smugmug plug-in creates an embed code that essentially relies on three different pieces of information that change from gallery to gallery:

  1. The gallery URL
  2. The gallery title
  3. The RSS feed for the gallery.

I have a partial solution: open the gallery, manually copy the URL, the page title, and the RSS feed into the clipboard, then use a keystroke to open Wordpress in another window and paste the post title, embed code, etc., into the appropriate spots.

This saves me time–but I’m greedy! I’d love to automate the process of copying the three items from the Smugmug gallery. The URL and the gallery title would be do-able, but the RSS feed is down at the bottom of the page tucked away among some other links. Is there a way to tell KM to grab the URL of that link by looking at page source or something?

Here’s a link to a sample Smugmug gallery:

Any help would be greatly appreciated! And if it can’t be done, KM is already saving me a lot of time.

I personally don’t know how to do this, but someone will come along who does.

I just wanted to tell you how awesome the photos look. I’m not a photographer, so I don’t know how much my opinion counts, but still… they look great! Awesome job.

1 Like

Thanks, Dan. This is the best I’ve ever felt about not getting a forum
question answered. It’s a labor of love–I appreciate your checking them
out!

I had to read that a few times before I understood. LOL!

And it's clear what you're doing is a labor of love.

1 Like

David,
You mentioned an RSS link. I don’t see one on the page but if you control-click or right-click on the link do you get an option to “Inspect Element”? That would give you some clues. Also does that RSS link only show up when you are logged in? If so you might want to copy the page source after you are logged in and past the properties for that element so we can see what might work to act on it.

Hey David,

You know how to comment and uncomment code?

Each line prefaced with a “#” character is commented-out (turned off). Of course some comment lines are just that – comments.

Open your gallery in Safari and run this from the Script Editor.app.

Progressively un-comment executable lines you want to see the value of in the result pane of the Script Editor.

If this is returning the data you want to see let me know, and we'll talk about how to use it.

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/25 21:00
# dMod: 2016/08/25 21:15
# Appl: Safari
# Task: Extract Data from smugmug.com Photo Gallery
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Extract, @Data, @Smugmug.com, @Photo, @Gallery
--------------------------------------------------------------------------------

tell application "Safari"
   tell document 1
      set docURL to URL
      # set docTitle to name
   end tell
end tell

# Image Link List
# set imageLinkList to safari_links("\\/i-.+", "*", "href")

# RSS Feed (maybe)
# set rssLink to safari_links("feed.*rss", "*", "href")
# if length of rssLink = 1 then set rssLink to item 1 of rssLink

--------------------------------------------------------------------------------
--» HANDLERS
--------------------------------------------------------------------------------
on safari_links(regexStr, tagName, tagType)
   set js to "function in_array (array, item) {
   for (var i=0; i < array.length; i++) {
      if ( array[i] == item ) {
         return true;}}
   return false;}
   var a_tags = document.getElementsByTagName('" & tagName & "');
   var href_array = new Array();
   var reg = new RegExp(/" & regexStr & "/i);
   for (var i=0; i < a_tags.length; i++) {
      var href = a_tags[i]." & tagType & ";
      if ( reg.test(href)) {
         if ( !in_array(href_array, href)) {
            href_array.push(href);}}}
 href_array;"
   
   try
      tell application "Safari" to set linkList to do JavaScript js in document 1
      if linkList = missing value then set linkList to {}
   on error
      set linkList to {}
   end try
   
   return linkList
   
end safari_links
--------------------------------------------------------------------------------

Extract Data from smugmug.com Photo Gallery.scptd.zip (10.0 KB)

Unlike the script text above the downloadable script has no commented-out lines.

-Chris

Tunes,

The RSS feed is there even if you’re not logged in. I’m not seeing it on mobile, though. I had to get into the desktop to check for sure (so maybe that’s why you didn’t see it?). On the desktop version of the site, the RSS icon is just to the right of the “Log In” link on a line of text links at the very bottom of the page.

In the page source, I see this line:

< link rel=“alternate” type=“application/rss+xml” title=“republictigersports > Varsity Soccer vs Monett Gallery RSS Feed” href="/hack/feed.mg?Type=gallery&Data=62646825_2xsbjp&format=rss200">

The actual link to the feed, which I need to pull for my Wordpress plug-in, is this:
https://republictigersports.smugmug.com/hack/feed.mg?Type=gallery&Data=62646825_2xsbjp&format=rss200

I’m guessing there’s a way to pull that first line, append it to the Smugmug URL, and get what I want. I may need to try it in the morning because I’m brain dead after another night of sports.

Thanks for your help!

Hey David,

My script gets the full RSS link correctly.

-Chris

Chris,

I ran your script after my last post and hadn’t had a chance to respond yet. Thank you so much! That’s perfect.

I guess the next step it to translate that into a KM macro. I may not be capable of doing that even with full sleep, but I know I’m not capable of it now. :wink: I’m going to look at it in the morning. Thank you again for your help. This is fantastic.

-David

Hey David,

Okay, run this from a Execute an AppleScript action in Keyboard Maestro.

If you don't need the image links you can delete the code related to them.

The script populates appropriate Keyboard Maestro variables with their designated data.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/08/25 21:00
# dMod: 2016/08/26 00:05
# Appl: Safari
# Task: Extract Data from smugmug.com Photo Gallery
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @Extract, @Data, @Smugmug.com, @Photo, @Gallery
--------------------------------------------------------------------------------

tell application "Safari"
   tell document 1
      set docURL to URL
      set docTitle to name
   end tell
end tell

# Image Link List
set imageLinkList to safari_links("\\/i-.+", "*", "href")

# RSS Feed (maybe)
set rssLink to safari_links("feed.*rss", "*", "href")
if length of rssLink = 1 then set rssLink to item 1 of rssLink

set AppleScript's text item delimiters to linefeed

# Get the data into Keyboard Maestro
tell application "Keyboard Maestro Engine"
   setvariable "docURL" to docURL
   setvariable "docTitle" to docTitle
   setvariable "imageLinkList" to imageLinkList as text
   setvariable "rssFeed" to rssLink
end tell

--------------------------------------------------------------------------------
1 Like

David,
It’s remarkable how the main subjects in the photos are so in focus they seem like they are superimposed on backgrounds of blur. How do you manage to do that? Impressive.

I guess you have noted Chris has got Skillz.

2 Likes

First of all, Chris – you’re amazing. Thank you so much! I think I have it all working correctly and it’s going to save me a ton of time. (And I also learned about using variables. I’d been just hacking little stuff together and hadn’t really delved that deeply into KM before. You’ve opened a new world for me.)

Tunes – the blurry background is mostly about the lens and not much about me. The only thing I do is manage to point the camera in the right direction and click at the right time (and delete the 70% of pictures that are NOT in focus). :wink:

Thanks to both of you for your help! I’m so glad I asked my question here.

1 Like

Well, I spoke too soon. I thought I’d gotten it into KM, but whatever I did is just repeating the same variables every time I run it. In other words, I tested it out on a football gallery. It worked and generated the post. But no matter what gallery I have open in Safari, the macro generates the information for that same football gallery.

Let me stipulate up front that this is probably some really dumb user error, but I can’t for the life of me figure out what it is (which is what makes it so dumb). I will also stipulate that I’m sort of figuring this out as I go along.

Chris, the script you wrote, I think, extracts information from whatever Smugmug gallery is open. Assuming that’s true, I must have mucked up the actions that generate my Wordpress post. After your script, I have the following actions:

Set Safari URL to http://www.republictigersports.com/wp-admin/post-new.php (New post editor)

Wait for Safari to Finish loading for at least 3 seconds

Insert text by pasting - “Photos: %Variable%docTitle%”

Type Keystroke (Tab)

Insert text by pasting: See the full album and order prints here.

[smugmug url="%Variable%rssFeed%" imagecount=“160” start=“1” num=“160” thumbsize=“Th” link=“lightbox” captions=“false” sort=“false” window=“true” smugmug=“true” size="X3”]

====================

And that’s it. I was taking a wild guess, but my guess was apparently wrong.

Hey David,

That's right – it works on the front Safari document.

This works for me given your original gallery page posted above.

If you don't have BBEdit then change the activate action to TextEdit or TextWrangler (or whatever text editor you like).

SmugMug Gallery Data Capture from Safari.kmmacros (4.9 KB)

I'm guessing you either have a timing issue or a focus issue (i.e. focus is not in the text box you need to paste into).

-Chris

I’m sorry to come back to this late, but I’ve been swamped and am just now getting a chance to address it. I’m continuing to have the weird issue that the macro is not over-writing the original variable we created.

If you remember, we tested the macro using a football photo gallery titled “Freshman Football vs Bolivar.” Now, no matter which Safari browser is open, even after quitting the KM engine, even after a reboot, I run the macro and IT STILL PASTES IN “Freshman Football vs Bolivar” and the related URL and RSS link. I cannot for the life of me figure out how that information still exists in memory somewhere, and I can’t see where the macro is calling that specific page’s info, as opposed to calling the top-most page on my browser.

The actual pasting is working – it’s going to the right spot and creating what I need to create. But it’s not pasting the correct info. It’s using the old variable information from the first time we ran it. Any idea what I’m doing wrong?

Aha! Upon further review, it was as obvious as I figured it must be. Somehow in the back-and-forth, I had removed the part of the code that told Keyboard Maestro to set the variables. Once I realized where the variables were set, it dawned on me what was going on. It’s all working now beautifully. Thanks again!

2 Likes

Every morning, I add a new chart photo to its SmugMug gallery. Examples like “euro”, “oil”, “yen”, etc. Is it possible to adjust this process to only share the new photo rather than the entire gallery?