Fetch, copy, and paste graphic from web page to Word

I am new to AppleScript and am trying to script the following scenario:

But,I have run into a block at step 4 in the following scenario:

  1. Open Safari and open a blank page in Word
  2. Fetch a succession of Web pages (up to 17)
  3. Each web page has several items, one of which is an image (these are weather maps). I am only interested in the image.
  4. Hover over image
  5. Ctrl-click
  6. Choose Copy Image from context menu (image is now in clipboard)
  7. Paste the image from the clipboard to the new document (Word)
  8. Repeat this 16 more times, addending each successive image to the same document
  9. Save the document

The block is at steps 4, 5, and 6.

I am able to get through step 3, but haven’t found a script that will do steps 4, 5 and 6. The aim is do all of this via AppleScript application without manual user input after step 1.

The file type of the graphic seems to be GIF

Thanks.

Hello @nephron,

Please provide links to the relevant weather pages.

This may (or may not) be much easier to do than you have envisioned, but I’d need to examine the source of the weather pages.

Not really weather but wind predictions, hourly, going out to 5 days: Here are a few of the links, which all follow the same format:

Nowcast (current):
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=00

6 hour forecast:
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=06

12 hour forecast:
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=12

The character near the end of the line, is actually an ampersand, =F&hr=12.

Thanks.

Model: MacBook Air
AppleScript: 2.4.3 (131.2)
Browser: Safari 537.71
Operating System: Mac OS X (10.8)

Hello @nephron,

That’s not too difficult. This should get you started.

set shCMD to text 2 thru -1 of "
tempDir=~/Downloads/\"Temp_Weather_Download_`date '+%Y.%m.%d.%H%M%S'`\"
mkdir \"$tempDir\";
cd \"$tempDir\";
baseURL=\"http://www.glerl.noaa.gov/res/glcfs/\";
curl -Ls --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' \"http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=00\" --url \"http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=06\" --url \"http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=12\" \\
| egrep -io \"<img src=.+\\.gif\" \\
| sed -E 's!<img src=.(.+\\.gif)!url = \"'\"$baseURL\"'\\1\"!' \\
| curl --remote-name-all -Ls --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' -K -
echo \"$tempDir\";
"
set tempDir to alias (POSIX file (do shell script shCMD))
tell application "Finder" to set imagePathList to files of tempDir as alias list

tell application "Microsoft Word"
	activate
	make new document
	tell active document
		repeat with imagePath in imagePathList
			set theRange to create range start 0 end 0
			make new inline picture at theRange with properties {file name:imagePath as text}
		end repeat
	end tell
end tell

Thanks, Chris-
I only recognize the code that looks like AppleScript. I did want to change the order so that Page 1 of Word document begins with most recent forecast, 00 h, and then, in sequence, 06, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 84, 96, 108, and 120 hr.
After adding a line, and changing the order, it did not work for me. I do not know who to manipulate that part of the script, which looks like shell script (but I am not sure).

A few hints would be welcome re how to do the above.

Thanks.

Hello @nephron,

Yes, everything in the variable shCMD is a shell script.

Those NOAA pages are easy to parse for their contained .gif files, so I’m using curl to download the source and then parsing it with egrep and sed and then downloading the found images with curl to a dated temporary directory.

From there it’s easy to load them up into a Microsoft Word document.

I rewrote the shell script, so the URL list is easier to read and maintain.

I fixed the problem of Word emplacing the images in reverse order.

The script is now commented.

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/02/09 21:32
# dMod: 2015/02/10 08:52
# Appl: Shell, Curl, Microsoft Word
# Task: Download weather images and insert them into a Word document.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Shell, @Curl, @Download, @Images, @Microsoft, @Word
-------------------------------------------------------------------------------------------

set shCMD to text 2 thru -1 of "
# Create a variable with the requisite URLs.
read -r -d '' urlList <<'EOF'
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=00
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=06
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=12
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=18
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=24
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=30
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=36
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=42
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=48
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=54
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=60
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=66
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=72
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=84
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=96
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=108
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=120
EOF
# Reformat into a proper config file format for curl.
urlList=$(sed -E 's!^(.+)!url = \"\\1\"!' <<< \"$urlList\");
# Produce a temporary directory to download images to and cd into it.
tempDir=~/Downloads/\"Temp_Weather_Download_`date '+%Y.%m.%d.%H%M%S'`\"
mkdir \"$tempDir\";
cd \"$tempDir\";
# Base URL for gif files found on web page.
baseURL=\"http://www.glerl.noaa.gov/res/glcfs/\";
curl -Ls --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' -K - <<< \"$urlList\" \\
| egrep -io \"<img src=.+\\.gif\" \\
| sed -E 's!<img src=.(.+\\.gif)!url = \"'\"$baseURL\"'\\1\"!' \\
| curl --remote-name-all -Ls --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' -K - ;
echo \"$tempDir\";
"
set tempDir to alias (POSIX file (do shell script shCMD))
# Reverse list - so Word will place in correct order.
tell application "Finder" to set imagePathList to reverse of (files of tempDir as alias list)

tell application "Microsoft Word"
	activate
	make new document
	tell active document
		repeat with imagePath in imagePathList
			set theRange to create range start 0 end 0
			make new inline picture at theRange with properties {file name:imagePath as text}
		end repeat
	end tell
end tell

-------------------------------------------------------------------------------------------

Thanks, Chris.
It is almost perfect. When I ran the script there was one peculiarity: the sequence was 00, 06, 108, 12, 120. from there on it was ok to 96. So, two of the maps are out of sequence and looking at the code I don’t see why that should be. But, I don’t know anything about shell scripting so am unable to see why this is happening. What do you think?

(could it be the prefatory comment lines?)

Thanks.

There must be something about the hours indicator at the end of the line for 108, which is truncated to 10, and 120, which is truncated to 12. I don’t know the fix for this.

Hey @nephron,

Hell. I missed that. The Finder sorts the files properly, but AppleScript is playing games with lexicographical sorting.

So. We’ll just rename the files, so they will sort properly.

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/02/09 21:32
# dMod: 2015/02/11 16:08
# Appl: Shell, Curl, Microsoft Word
# Task: Download weather images and insert them into a Word document.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Shell, @Curl, @Download, @Images, @Microsoft, @Word
-------------------------------------------------------------------------------------------

set shCMD to text 2 thru -1 of "
# Create a variable with the requisite URLs.
read -r -d '' urlList <<'EOF'
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=00
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=06
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=12
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=18
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=24
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=30
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=36
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=42
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=48
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=54
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=60
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=66
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=72
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=84
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=96
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=108
http://www.glerl.noaa.gov/res/glcfs/glcfs.php?lake=m&ext=wn&type=F&hr=120
EOF
# Reformat into a proper config file format for curl.
urlList=$(sed -E 's!^(.+)!url = \"\\1\"!' <<< \"$urlList\");
# Produce a temporary directory to download images to and cd into it.
tempDir=~/Downloads/\"Temp_Weather_Download_`date '+%Y.%m.%d.%H%M%S'`\"
mkdir \"$tempDir\";
cd \"$tempDir\";
# Base URL for gif files found on web page.
baseURL=\"http://www.glerl.noaa.gov/res/glcfs/\";
curl -Ls --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' -K - <<< \"$urlList\" \\
| egrep -io \"<img src=.+\\.gif\" \\
| sed -E 's!<img src=.(.+\\.gif)!url = \"'\"$baseURL\"'\\1\"!' \\
| curl --remote-name-all -Ls --user-agent 'Opera/9.70 (Linux ppc64 ; U; en) Presto/2.2.1' -K - ;
echo \"$tempDir\";
"
set tempDir to alias (POSIX file (do shell script shCMD))

# Fix file names, so they will sort correctly.
tell application "Finder"
	set fileList to files of tempDir as alias list
	repeat with theFile in fileList
		set AppleScript's text item delimiters to {"+", "."}
		set _name to text items of (get name of theFile)
		if length of item 2 of _name is 2 then
			set (item 2 of _name) to "0" & (item 2 of _name)
		end if
		set AppleScript's text item delimiters to "."
		set _name to _name as text
		set name of theFile to _name
	end repeat
end tell

# Reverse list - so Word will place in correct order.
tell application "Finder" to set imagePathList to reverse of (files of tempDir as alias list)

tell application "Microsoft Word"
	activate
	make new document
	tell active document
		repeat with imagePath in imagePathList
			set theRange to create range start 0 end 0
			make new inline picture at theRange with properties {file name:imagePath as text}
		end repeat
	end tell
end tell

-------------------------------------------------------------------------------------------

Thanks, Chris. It does the job perfectly. Goes to show that AppleScript needs help with certain tasks. After I change the left and right margin in Word to 0.5 inch, the script pulls in one graphic per page, which is what I want. For some reason these wind forecast images have about 1 inch of white space on the left and right sides. I have written to the GLCFS webmeister about this (since I would like to trim those out so that the image can take up as much space as possible within the 0.5 inch side margins)…

Thanks, again.