Getting Image and Text on the clipboard.

Hi, I am trying to copy one image and several blocks of text from a webpage using Applescript. It is working (in a fashion) but I have not found a method of doing the Image and the Text at the same time. My long winded method is as follows:
I save a copy of the image to a temp file on the desktop (FOO).
Find the correct text and place into variables.
Set the clipboard to the saved image (from my desktop).
Create a temp text file - paste the clipboard image to it.
set the clipboard to the text variables - paste them to the text file.
Copy the entire text file (image and text) - manually paste to where it needs to be.

The end result should be an image with several blocks of text underneath on the clipboard.

Can someone help make this slicker please? Can the image be stored as a variable too??

I can’t show the actual website here (I have to sign in), so I have just picked a website at random for the cut down example below.


-- Example website picked at random for example
-- https://cccbr.org.uk/bellringing/what-is-bell-ringing/

-- Image URL
set FullURL to "https://cccbr.org.uk/wp-content/uploads/2018/04/bampton.jpg"

tell application "Safari"
	-- Save Image file from report to desktop as (FOO.JPG)
	set newname to POSIX path of ((path to desktop) as text) & "foo.jpg"
	set status to (do shell script "curl -s -o " & newname & space & FullURL's quoted form)
	
	-- Gets all of the report and copies to variable
	set theSource to source of the document 1
end tell

-- Converts the Report (Source) to a text variable
set FullRecord to do shell script "/bin/echo " & quoted form of theSource & " | /usr/bin/textutil -stdin -stdout -format html -inputencoding iso-8859-1 -convert txt -encoding UTF-8"

--Find the start and end of the comments
set FirstPgCommentsStart to offset of "The origins" in FullRecord
set FirstPgCommentsEnd to offset of "a method" in FullRecord

-- Comments into variable
set myComments to text (FirstPgCommentsStart) thru (FirstPgCommentsEnd + 8) of FullRecord


-- ** Need a better method from here **

-- Put the Image on the clipboard
try
	set the clipboard to (read alias "Macintosh HD:Users:Me:Desktop:foo.jpg" as TIFF picture)
end try

-- Create TextEdit document as temp storage for both Image and Text
tell application "TextEdit"
	activate
	set newDoc to make new document
end tell

tell application "System Events" to keystroke "v" using command down

delay 1
set the clipboard to myComments

tell application "System Events" to keystroke "v" using command down
delay 0.5
tell application "System Events" to keystroke "a" using command down
delay 0.5
tell application "System Events" to keystroke "c" using command down

tell application "Safari"
	activate
	display dialog "Image and Text have been added to the Clipboard."
end tell



You can’t paste the images into TextEdit documents. Use other application. Pages.app, for example.

Hi KniazidisR.

You can paste images into TextEdit’s RTF documents, which are presumably what Niscors has configured for its default documents.

Yes, it’s currently working but seems to be soooo clunky with the textedit windows opening and closing on screen etc… and fairly slow.
I was hoping there is a much smoother way to do it??

Hi Niscors.

I’m afraid putting separate text and image sources onto the clipboard directly is beyond my immediate competence. :frowning: But with regard to your method, the pasting-the-text-into-the-document stage can be eliminated by creating the document with the text as a property. On my Mojave machine, at least, the document is created with the insertion point in front of the first character of the text, so it’s then only necessary to paste in the image. Something like this for the second half of the script:

-- ** Need a better method from here **

-- Put the Image on the clipboard
try
	set the clipboard to (read POSIX file newname as TIFF picture)
end try

-- Create TextEdit document as temp storage for both Image and Text
tell application "TextEdit"
	activate
	set newDoc to (make new document with properties {text:myComments})
end tell
-- The document has (with any luck) been created with the insertion point
-- in front of the text, so pasting should insert the image there.
tell application "System Events" to keystroke "vac" using command down

tell application "Safari"
	activate
	display dialog "Image and Text have been added to the Clipboard."
end tell

Thanks Nigel, that is smoother.

Any idea why it opens 2 textedit windows - one (untitled) which is empty and one (untitled 2) which has the image and text in it ??

The empty one will be the default document TextEdit opens when it’s activated from scratch. You could possibly set this document’s text to myComments instead of making a new one. But in case you already have another document open that you don’t want to have overwritten, a safer course would be to use ‘run’ or ‘launch’ instead of ‘activate’, make the new document, and then use ‘activate’ to bring Text Edit to the front:

tell application "TextEdit"
	run
	make new document with properties {text:myComments}
	activate
end tell