Q: AppleScript to create thumbnail of selected file with hyperlink to the URL of the file
Hi,
I need an AppleScript to copy to the clipboard a thumbnail of a selected file (s. image 1 below) with a hyperlink to the URL of that selected file. 
I have a script that allows me to get the URL of the selected file and copy it to the clipboard. But then I have to paste the URL in my Document, go back to finder, make a screenshot of the file's thumbnail, paste that screenshot and insert the (manually copied) URL of the file (chosing the context menu of the image) (s. image 2 below):
Is there a way to automate this?
I could make the screenshot manually, that is actually no problem, but then I'd need to attribute this screenshot to a clipboard 1, go back and get the URL of the file and attribute it to clipboard 2 and then go to my document and paste clipboard 1 first, chose the context menu 'hyperlink' and paste clipboard 2.
I'd be thankful for help !!
(I am a high school language teacher and this is a daylily repetitive task)
Regards,
Kai-Uwe
MacBook Air, OS X El Capitan (10.11.6)
Posted on Sep 11, 2016 3:16 PM
Here is version 2.1 of my previous script. I does the following:
- Eliminates the choose file prompt. You now select the files to process in the Finder, and then run the script.
- All comments are gone from the HTML and inherently, the ODT documents.
- Removed custom.css comment
- Changed the pandoc output from HTML5 to HTML
- Now excludes the conditional comment for Internet Explorer 9 compatibility
- Now excludes the html5shiv JavaScript link
- Cannot fix thumbnail outline in LibreOffice
- LibreOffice issue, not script production, or controllable factor.
- Thumbnail outline persists after manually disabling border outline property for the image.
AppleScript
-- ql_link.applescript
-- Writes custom.css, and markdown files. The latter contains content
-- that enables hyperlinked thumbnails with hover filenames shown.
-- When processing is complete, the third-party pandoc (pandoc.org) application is used
-- to convert markdown content to an HTML web page.
--
-- Optional: use the LibreOffice command-line tool soffice to convert the
-- HTML content to either ODT, or DOCX documents. This will preserve
-- all HTML functionality including hyperlinks on thumbnails and hover text
-- The following commands have wrapped, it is a single-line invocation. Output work.odt, or work.docx.
--
-- /Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to odt --outdir ~/Desktop work.html
-- /Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to docx:"MS Word 2007 XML" --outdir ~/Desktop work.html
--
-- Version 2.1. • Rewrite. Switched from LaTeX to markdown metadata for improved f(x).
-- • Include custom CSS to assist pandoc generation of horizontal html content.
-- • allows horizontal 200 px thumbnails that wrap on web page.
-- • A single-click on a thumbnail in Safari 9.1.3 (or Firefox 48.0.1) will open
-- that document in its default application, or Finder window.
-- • Replaced choose file with Finder selection(s)
-- • All comments now suppressed in html and odt documents.
-- VikingOSX, Sept 18, 2016, Apple Support Communities
property default_loc : ((path to desktop) as text) as alias
property qmesg : "Choose file(s) to generate thumbnails"
property metafile : (path to desktop as Unicode text) & "meta.md"
property mfile : POSIX path of metafile
property cssfile : (path to desktop as Unicode text) & "custom.css"
property cfile : POSIX path of cssfile
property thumbpath : (path to desktop as text) & "Thumbs:"
property fileCnt : missing value
use scripting additions
tell application "Finder"
if exists file metafile then
delete file metafile
end if
if exists file cssfile then
delete file cssfile
end if
if not (exists folder thumbpath) = true then
make new folder at (path to desktop as text) with properties {name:"Thumbs"}
end if
end tell
-- set inPath to (choose file with prompt qmesg default location default_loc with multiple selections allowed without invisibles and showing package contents)
-- Finder selected documents are made into a file list
tell application "Finder"
set inPath to the selection as alias list
end tell
-- create new cssfile and initialize it
set fcRef to openFileHandle(cssfile, true)
init_cssfile(fcRef)
set fRef to openFileHandle(metafile, true)
if fRef is false then return
set fileCnt to (count of inPath)
repeat with afile in inPath
tell application "Finder"
set ImgPath to ""
set theURL to (afile's URL) as «class utf8»
set theName to (afile's name) as «class utf8»
-- because qlmanager generates name.extension.png image names!!
set ImgPath to POSIX path of (thumbpath & theName & ".png") as «class utf8»
set inFile to (afile's POSIX path)'s quoted form
end tell
try
-- use the QuickLook manager to generate a 200 pixel icon. Better to downsample.
do shell script "/usr/bin/qlmanage -tif 0.78 -s 256 " & inFile & " -o " & POSIX path of thumbpath & " >& /dev/null"
tell application "Finder" to set imgURL to URL of (POSIX file ImgPath as alias)
log imgURL
write_metadata(fRef, theURL, theName, imgURL)
on error errmsg number errnbr
my error_handler(errnbr, errmsg)
close access the fRef
return
end try
end repeat
close access the fRef
do shell script "/usr/local/bin/pandoc -H " & cfile & " -f markdown -t html -o ~/Desktop/work.html " & mfile
do shell script "open ~/Desktop/work.html"
display dialog (fileCnt as text) & " records written. Document produced."
set fileCnt to (0 as integer)
return
on openFileHandle(filePath, overwrite)
try
set the filePath to the filePath as Unicode text
set the fp to open for access file filePath with write permission
if overwrite is true then set eof of the fp to 0 -- truncate file
on error errmsg number errnbr
try
close access the fp
set fp to (open for access filePath with write permission)
if overwrite is true then set eof of fp to 0
on error
my error_handler(errnbr, errmsg)
return false
end try
end try
return fp
end openFileHandle
on init_cssfile(fh)
-- Override the pandoc generated HTML
-- this CSS will be provided to pandoc on its command-line with the -H flag
set eof of fh to 0
write "<style type=\"text/css\">" & linefeed as «class utf8» to fh starting at eof
write "img {display:inline-block;margin:5px 20px;padding:5px;}" & linefeed as «class utf8» ¬
to fh starting at eof
write " p {display:inline;margin:0;padding:0;}" & linefeed as «class utf8» ¬
to fh starting at eof
write "</style>" as «class utf8» to fh starting at eof
close access fh
return
end init_cssfile
on write_metadata(fh, aURL, aName, theImgURL)
-- markdown syntax to make hyperlinked thumbnails
write "[](" & aURL & ")" & ¬
linefeed & linefeed to fh starting at eof as «class utf8»
return
end write_metadata
on error_handler(nbr, msg)
return display alert "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler
op
Posted on Sep 18, 2016 9:43 AM





