kaiuweheinz

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. Bildschirmfoto 2016-09-11 um 23.57.44.png

 

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):

 

Bildschirmfoto 2016-09-12 um 00.06.31.png

 

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

Close

Q: AppleScript to create thumbnail of selected file with hyperlink to the URL of the file

  • All replies
  • Helpful answers

Previous Page 2 of 4 last Next
  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 12:15 PM in response to Hiroto
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 12:15 PM in response to Hiroto

    I have just noticed that when I try to save the thumbnails included in the output file 'out.html' they all come without extension (jpeg or png) - could this be the reason they aren't displayed correctly when opening the out.html within OOffice?

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 12:17 PM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 12:17 PM in response to VikingOSX

    Dear VikingOSX,

     

    I have erroneously posted two replies in one post - one to you and one to you - (I am new to all this here). I would appreciate if you could give it a glance.

     

    Thank you so much in advance,

    Kai-Uwe

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 12:39 PM in response to kaiuweheinz
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 12:39 PM in response to kaiuweheinz

    Just to give you an idea of what this is all about -

     

    this is what my protocols look like with just hyperlinks:

    Bildschirmfoto 2016-09-14 um 21.29.32.png

     

    and this is what they looked like when I manually created clickable thumbnails:

    Bildschirmfoto 2016-09-14 um 21.37.03.png

     

    Kind regards,

    Kai-Uwe

  • by VikingOSX,

    VikingOSX VikingOSX Sep 14, 2016 12:59 PM in response to kaiuweheinz
    Level 7 (21,014 points)
    Mac OS X
    Sep 14, 2016 12:59 PM in response to kaiuweheinz

    I do not have any spaces in my metafile declaration because my boot drive is not named Macintosh HD. You will have to replace the reference to metafile as the following to escape the white space in your bootdrive name:

     

    property metafile : (path to desktop as text) & "meta.tex"

    property mfile : (metafile's POSIX path)'s quoted form

     

    on write_metadata(fh, aURL, imagePath)

         set imagecnt to (do shell script "[[ -s " & mfile & " ]] && egrep -c includegraphics " & mfile & " || echo \"0\"") as integer

    ...

    end write_metadata


    You will need to use this technique for the metafile argument to pandoc too.


    do shell script "/usr/local/bin/pandoc -s -f latex -o ~/Desktop/work.docx " & mfile

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 2:09 PM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 2:09 PM in response to VikingOSX

    Dear VikingOSX,

     

    unfortunately it tells me this:

    Bildschirmfoto 2016-09-14 um 22.51.28.png

    ("The variable "fp" is not defined.")

     

    The exact Script I ran after deleting "meta.tex" and after restarting the ScriptEditor is this:

    Dear VikingOSX,

     

    unfortunately it tells me this:

     

    ("The variable "fp" is not defined.")

     

    The exact Script I ran after deleting "meta.tex" and after restarting the ScriptEditor is this:

     

    property default_loc : ((path to desktop) as text) as alias

    property qmesg : "Choose file(s) to generate thumbnails"

    property metafile : (path to desktop as text) & "meta.tex"

    property mfile : (metafile's POSIX path)'s quoted form

    property thumbpath : (path to desktop as text) & "Thumbs:"

    property fileCnt : missing value

     

    tell application "Finder"

      if exists file metafile then

      delete file metafile

      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

    -- create the empty metafile and return a file handle used to write to it

    set fRef to openMetaFile(metafile)

    -- initialize with the preamble LaTeX stuff

    init_metadata(fRef)

     

    set inPath to (choose file with prompt qmesg default location default_loc with multiple selections allowed without invisibles and showing package contents)

     

    set fileCnt to (count of inPath)

    repeat with afile in inPath

      tell application "Finder"

      set ImgPath to ""

      set theURL to (afile's URL) as text

      set theImgName to afile's name & ".png"

      set ImgPath to POSIX path of (thumbpath & theImgName)

      set inFile to (afile's POSIX path)'s quoted form

      end tell

      try

      write_metadata(fRef, theURL, ImgPath)

      -- use the QuickLook manager to generate a 128 pixel icon at 90% scale

      do shell script "/usr/bin/qlmanage -tif 0.9 -s 128 " & inFile & " -o " & POSIX path of thumbpath & " >& /dev/null"

      on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      set fileCnt to 0

      return

      end try

    end repeat

    write "\\end{document}" & linefeed to fRef starting at eof

    close access fRef

    -- do shell script "/usr/local/bin/pandoc -s -f latex -o ~/Desktop/work.odt " & POSIX path of metafile

    -- do shell script "/usr/local/bin/pandoc -s -f latex -o ~/Desktop/work.docx " & POSIX path of metafile

    do shell script "/usr/local/bin/pandoc -s -f latex -o ~/Desktop/work.docx " & mfile

    display dialog (fileCnt as text) & " records written. Document produced."

    set fileCnt to 0

    return

     

    on openMetaFile(filePath)

      -- influenced by twtwtw from https://discussions.apple.com/thread/4256380?tstart=0

      try

      set fp to (open for access file filePath with write permission)

      on error errmsg number errnbr

      if errnbr = -49 then

      close access fp

      set fp to (open for access filePath with write permission)

      else

      my error_handler(errnbr, errmsg)

      return false

      end if

      end try

      return fp

    end openMetaFile

     

    on init_metadata(fh)

      -- latex preamble

      write "\\documentclass[10pt, twocolumn]{article}" & linefeed to fh starting at eof

      write "\\usepackage{url}" & linefeed to fh starting at eof

      write "\\usepackage{graphicx}" & linefeed to fh starting at eof

      write "\\" & linefeed to fh starting at eof

      write "\\setmainfont{Helvetica}" & linefeed to fh starting at eof

      write "\\begin{document}" & linefeed to fh starting at eof

      return

    end init_metadata

     

    on write_metadata(fh, aURL, imagePath)

      -- attempt to control quantity of images on a page

      set imagecnt to (do shell script "[[ -s " & mfile & " ]] && egrep -c includegraphics " & mfile & " || echo \"0\"") as integer

      -- every four images we introduce a new page

      if imagecnt > 0 and ((imagecnt mod 4) as integer) = 0 and imagecnt < fileCnt then

      write "\\newpage" & linefeed to fh starting at eof

      end if

      -- write data to it

      write "\\url{" & aURL & "}" & linefeed to fh starting at eof

      write "\\\\\\\\" & linefeed to fh starting at eof

      write "\\includegraphics{" & imagePath & "}" & linefeed to fh starting at eof

      write "\\\\\\\\" & linefeed to fh starting at eof

      return

    end write_metadata

     

    on error_handler(nbr, msg)

      return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

    end error_handler

     

     

     

    Thank you in advance,

    Kai-Uwe

  • by Hiroto,

    Hiroto Hiroto Sep 14, 2016 4:27 PM in response to kaiuweheinz
    Level 5 (7,306 points)
    Sep 14, 2016 4:27 PM in response to kaiuweheinz

    Well then, it seems OpenOffice Writer and Microsoft Word do not paste html data. As I mentioned earlier, LibreOffice does paste it, though.

     

    Whilst it is possible to amend the script to yield thumbnail image files in any given directory or even put different type(s) of data in the clipboard so that those applications could paste, I'd like to know the following first -

     

    1) Can you paste the resulting data in the clipboard to a TextEdit rich text document correctly? If you can, does copying contents of the TextEdit document and pasting them into OO Writer and/or MS Word document yield desired result?

     

    2) What happens if you open the resulting html file by Safari browser, select all contents, copy and paste them into OO Writer and/or MS Word document? Does it yield desired result?

     

     

    * Under OS X 10.6.8, I can paste html data into a TextEdit rich text document correctly and copying contents of the TextEdit rich text document puts rtfd data in the clipboard. Also copying contents of Safari window puts rtfd data in the clipboard. If you can paste the rtfd data into OO Writer and/or MS Word document correctly, modifying the script so as to put rtfd data along with html data in the clipboard should solve the problem.

     

    Regards,

    H

  • by VikingOSX,

    VikingOSX VikingOSX Sep 14, 2016 4:34 PM in response to kaiuweheinz
    Level 7 (21,014 points)
    Mac OS X
    Sep 14, 2016 4:34 PM in response to kaiuweheinz

    Here is an update to my original code post that has been tested on OS X 10.11.6 with a boot drive using Macintosh HD. It also incorporates some logic changes in the write_metadata function, that actually now inserts newpage commands into the metafile after every four images. I have tested this successfully with up to 13 files of varying types, and it does not break in any do shell script syntax now.

     

    Code:

    -- qlthumb.applescript

    -- Prompt user for files and write LaTeX metadata that is transformed into

    -- DOCX or ODT documents via third-party pandoc utility

    -- Version 1.2

    -- VikingOSX, Sept 14, 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 text) & "meta.tex"

    property mfile : POSIX path of metafile

    property thumbpath : (path to desktop as text) & "Thumbs:"

    property fileCnt : missing value

     

    tell application "Finder"

      if exists file metafile then

      delete file metafile

      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

     

    -- create the empty metafile and return a file handle used to write to it

    set fRef to openMetaFile(metafile)

    -- initialize with the preamble LaTeX stuff

    init_metadata(fRef)

     

    set inPath to (choose file with prompt qmesg default location default_loc with multiple selections allowed without invisibles and showing package contents)

     

    set fileCnt to (count of inPath)

    repeat with afile in inPath

      tell application "Finder"

      set ImgPath to ""

      set theURL to (afile's URL) as text

      set theImgName to afile's name & ".png"

      set ImgPath to POSIX path of (thumbpath & theImgName)

      set inFile to (afile's POSIX path)'s quoted form

      end tell

      try

      write_metadata(fRef, theURL, ImgPath)

      -- use the QuickLook manager to generate a 128 pixel icon at 90% scale

      do shell script "/usr/bin/qlmanage -tif 0.9 -s 128 " & inFile & " -o " & POSIX path of thumbpath & " >& /dev/null"

      on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      set fileCnt to 0

      return

      end try

    end repeat

    write "\\end{document}" & linefeed to fRef starting at eof

    close access fRef

    -- do shell script "/usr/local/bin/pandoc -s -f latex -o ~/Desktop/work.odt " & metafile

    do shell script "/usr/local/bin/pandoc -s -f latex -o ~/Desktop/work.docx " & mfile

    display dialog (fileCnt as text) & " records written. Document produced."

    set fileCnt to (0 as integer)

    return

     

    on openMetaFile(filePath)

      -- influenced by twtwtw from https://discussions.apple.com/thread/4256380?tstart=0

      try

      set fp to (open for access file filePath with write permission)

      on error errmsg number errnbr

      if errnbr = -49 then

      close access fp

      set fp to (open for access filePath with write permission)

      else

      my error_handler(errnbr, errmsg)

      return false

      end if

      end try

     

      return fp

     

    end openMetaFile

     

    on init_metadata(fh)

      -- latex preamble

      write "\\documentclass[10pt, twocolumn]{article}" & linefeed to fh starting at eof

      write "\\usepackage{url}" & linefeed to fh starting at eof

      write "\\usepackage{graphicx}" & linefeed to fh starting at eof

      write "\\" & linefeed to fh starting at eof

      write "\\setmainfont{Helvetica}" & linefeed to fh starting at eof

      write "\\begin{document}" & linefeed to fh starting at eof

      return

    end init_metadata

     

    on write_metadata(fh, aURL, imagePath)

     

      -- attempt to control quantity of images on a page

      try

      set imagecnt to (do shell script "grep -c 'includegraphics' " & mfile) as integer

      on error

      -- avoid issue where match above fails and returns "0"

      set imagecnt to 0 as integer

      end try

      -- every four images we introduce a new page

      log imagecnt

      if imagecnt > "0" then

      if ((imagecnt mod 4) as integer) = 0 and imagecnt < fileCnt then

      write "\\newpage" & linefeed to fh starting at eof

      end if

      end if

     

      -- write data to it

      write "\\url{" & aURL & "}" & linefeed to fh starting at eof

      write "\\\\\\\\" & linefeed to fh starting at eof

      write "\\includegraphics{" & imagePath & "}" & linefeed to fh starting at eof

      write "\\\\\\\\" & linefeed to fh starting at eof

     

      return

     

    end write_metadata

     

    on error_handler(nbr, msg)

      return display alert "[ " & nbr & " ] " & msg as critical giving up after 10

    end error_handler

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 4:46 PM in response to Hiroto
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 4:46 PM in response to Hiroto

    Dear Hiroto,

     

    I can paste them into a TextEdit right text document. But not 100% correctly:

    Bildschirmfoto 2016-09-15 um 01.31.57.png

    It loos perfect but when clicking on a thumbnail, the documents doesn't get opened, but the specific file gets selected in the finder window:

    Bildschirmfoto 2016-09-15 um 01.32.07.png

    When copying the thumbnails into OOffice, only file path, not the thumbnail is transmitted:

    Bildschirmfoto 2016-09-15 um 01.32.21.png

    and it isn't a link any more, i.e. I cannot click on the path in order to open the source document. The same happens when copying from Safari to TextEdit.

     

    I am actually working with NeoOffice a paid program derived from OOffice because both LibreOffice and OpenOffice turned out to be less stable in front of the classes. There were always instabilities or flaws when working with tables in writer (which I do constantly) so I changed. I reconsider however re-installing LibreOffice in order to be able to copy from there to OOffice.

     

    As described in my earlier post, if I copy and paste from Safari to OOffice/MSWord I get the same result.

     

    Grateful for your advice,

    Kai-Uwe

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 4:51 PM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 4:51 PM in response to VikingOSX

    The error ("The variable "fp" is not defined.") doesn't occur anymore, yet there appeared another error message. Here is what it says:

    Bildschirmfoto 2016-09-15 um 01.49.34.png

     

    Should I create such a folder manually?

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 4:56 PM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 4:56 PM in response to VikingOSX

    But: for the first time there are thumbnails in the desktop "Thumbs" folder and the file "meta.tex" has more content! So it seems close to accomplishment!

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 5:06 PM in response to Hiroto
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 5:06 PM in response to Hiroto

    Here is what I got when I copied from "out.html" (from Safari) to LibreOffice:

    Bildschirmfoto 2016-09-15 um 02.03.36.png

     

    So here too the thumbnail is not transmitted. I used the version LibreOffice 5.2.1 - on which version did you yield your results?

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 5:19 PM in response to kaiuweheinz
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 5:19 PM in response to kaiuweheinz

    I just created the folder 'pandoc' manually but the error message remains the same.

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 5:21 PM in response to kaiuweheinz
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 5:21 PM in response to kaiuweheinz

    It says:

    Bildschirmfoto 2016-09-15 um 02.20.58.png

  • by VikingOSX,

    VikingOSX VikingOSX Sep 14, 2016 5:37 PM in response to kaiuweheinz
    Level 7 (21,014 points)
    Mac OS X
    Sep 14, 2016 5:37 PM in response to kaiuweheinz

    My pandoc was installed by homebrew package manager into the /usr/local/bin directory as pandoc — hence the path that I used as /usr/local/bin/pandoc. You need to change this to the path where your pandoc executable is located.

     

    Once you do that, then you can generate your Word or ODT document, as all else is complete at that point in the script.

     

    Should the script abort, that will leave you with an open meta.tex file, and you will have to quit the script editor to unlink that file in use bit. Then move meta.tex to the trash, and restart the script.

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Sep 14, 2016 11:57 PM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Sep 14, 2016 11:57 PM in response to VikingOSX

    Thank you very much. I followed your advice and it did work once but then I got this:

    Bildschirmfoto 2016-09-15 um 08.53.10.png

Previous Page 2 of 4 last Next