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

first Previous Page 5 of 5
  • by VikingOSX,

    VikingOSX VikingOSX Oct 2, 2016 12:05 PM in response to kaiuweheinz
    Level 7 (21,421 points)
    Mac OS X
    Oct 2, 2016 12:05 PM in response to kaiuweheinz

    I have updated the Protokoll AppleScript to version 3.

     

    It will now prompt you for the Protokoll name, which can be whitespace, and UTF8 characters. Further, it prompts you for whether you want this Protokoll folder written to the Desktop (local), or onto ejectable, mounted media. I am demoting spaces in the Protocol name to underscores (Unterstrichen) before I create the folder. If you are using ejectable media, I will automatically present you with a list choice of those that are mounted (but not Time Machine) to your Desktop. I do not change any whitespace in mounted media, and the script works fine if the volume name contains white space (and UTF8 characters).

     

    Aside from more condition and error checking, the script essentially copies the Finder selected files into the Protokoll folder, embeds the thumbnail images into the final ODT document, and resolves the thumbnail hyperlinks to the respective files located in the Protokoll folder. The script will now automatically open the ODT document (if it exists) with LibreOffice, and I have commented out the equivalent behavior with NeoOffice, as it was not installed for testing. I discovered that if LibreOffice was already running, any command-line attempts to convert from HTML to ODT were silently ignored, so I have placed a check beforehand that will quit LibreOffice if it is running.

     

    Finally, there is a cleanup() routine that runs at the beginning and the end of the script that deletes all of the temporary files generated that produce the ODT document. I do not remove any Protokoll folders.

     

    I am not infallible, but there are two days of testing with local (Desktop) and mounted media Protokoll folder scenarios and destinations on both El Capitan and macOS Sierra. It seems to run properly as written.

     

    AppleScript

     

    -- protokoll3.applescript

    --

    -- Select the Finder files for processing. The script will then produce an

    -- ODT document in the selection choice location of the Protokoll folder,

    -- with the respective embedded file thumbnails, and their resolved file

    -- hyperlinks. Note: Protokoll name can be entered with spaces in its name,

    -- but script will demote spaces to underscores (Unterstrichen).

    --

    -- Revision: 3.0 • Rewritten for prompted protokoll folder creation on local, and ejectable

    --                         media

    --                       • Protokoll name

    --                         - UTF8. Spaces changed to underscores via Objective-C

    --                         - Simplifies coding with non-whitespace folder names

    --                       • More file checking and error handling

    --                       • Added a cleanup() handler before and after script execution

    --                         - does not remove any Protocol folder or contents

    --                         - removes temporary files and thumbnail folder from Desktop

    --                       • Will quit LibreOffice if it is running before attempting to

    --                         generate ODT document from HTML on command-line.

    --

    -- Requires:      • Pandoc 1.15 or later (tested with 1.17.2)

    --                      • LibreOffice v5+ (tested with v5.2.2.2)

    --                      • AppleScript 2.4+ (Yosemite or later, for direct Objective-C use)

    --                      • Script tested on El Capitan 10.11.6, and macOS Sierra 10.12.0

    --

    -- Optional:      • NeoOffice Viewer

    --                      • Ejectable thumb drive, or SD?C media card

    -- VikingOSX, October 2, 2016, Apple Support Community

     

    property VolumeName : missing value

    property ProtokollFolder : missing value

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

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

    property ODT_File : "out.odt"

    property HTML_File : "out.html"

    property workHTML : POSIX path of ((path to desktop as text) & HTML_File)

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

    property cssfile : (path to desktop as Unicode text) & "custom.css"

    property dataURI : "data:image/png;base64,"

    property fileCnt : 0 as integer

    property msg : "Please enter protokoll name for output folder." & return & ¬

      "Spaces in names will be changed to '_'."

     

    use scripting additions

    use framework "Foundation"

    use AppleScript version "2.4" -- Yosemite or later (for direct Objective-C usage)

     

    try

      cleanup() -- remove all temporary files from last run that may exist

      -- stop script if user has not preselected Finder files before script is run.

      tell application "Finder"

      -- Finder selected documents are made into a file list

      set inPath to the selection as alias list

      if inPath = {} then

      display alert "Finder" message "You must select Finder items before executing this script." as critical giving up after 10

      return

      end if

      end tell

     

      -- get name of output protocol folder

      set ProtokollFolder to text returned of (display dialog msg default answer ¬

      "" with title "Protokoll") as «class utf8»

     

      if ProtokollFolder contains space then

      -- change spaces in protokoll name to underscore

      set ProtokollFolder to no_whitespace(ProtokollFolder)

      end if

     

      set MediaChoice to (choose from list {"Mounted", "Desktop"} with title "Protokoll Location" with prompt "Choose local drive, or mounted media." default items {"Desktop"} without multiple selections allowed and empty selection allowed)

      if MediaChoice is false then error number -128

     

      -- Request location to make named folder after the protocol provided

      tell application "Finder"

      set outputFolder to (LocalPath & ProtokollFolder) as text

      if MediaChoice contains "Desktop" and ¬

      not (exists folder outputFolder) then

      set outputFolder to (make new folder at LocalPath with properties {name:ProtokollFolder}) as text

      else if MediaChoice contains "Mounted" then

      get name of (every disk whose ejectable is true and its name is not in {"Time Machine", "TIME MACHINE"}) as list

      set VolumeName to (choose from list (result) with title "Mounted Volumes" default items "" without multiple selections allowed and empty selection allowed) as «class utf8»

      if VolumeName is false then return -- cancel button

     

      if VolumeName contains space then

      end if

      set VolumeName to ("/Volumes/" & VolumeName & "/") as text

      if not (exists outputFolder) then

      set outputFolder to (make new folder at (VolumeName as POSIX file) with properties {name:ProtokollFolder}) as text

      end if

      end if

      end tell

    on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      return

    end try

     

    tell application "Finder"

      -- place Finder selected files in the output Protocol folder

      repeat with afile in inPath

      duplicate afile to folder (outputFolder as alias) with replacing

      end repeat

      -- make a list of the files in the protokoll folder

      set mediaList to (every item in entire contents of folder (outputFolder as alias) whose name extension is not in {"html", "odt"}) as alias list

      if mediaList = {} then

      display alert "Finder" message "No files found in mounted media folder." as critical giving up after 5

      return

      end if

     

      set fileCnt to (count of mediaList) as integer

      make new folder at LocalPath with properties {name:"Thumbs"}

     

    end tell

     

    -- get metadata file handle for writing

    set fRef to openFileHandle((POSIX path of metafile), true)

    if fRef is false then return

     

    -- generate the thumbnails from each file, and write to the metafile.

    repeat with afile in mediaList

      tell application "Finder"

      set theURL to (afile's URL) as «class utf8»

      set theName to (afile's name) as «class utf8»

      set ImgPath to ""

      -- 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 into the thumbnail folder.

      do shell script "/usr/bin/qlmanage -ti -s 200 " & inFile & " -o " & POSIX path of (thumbpath as alias) & " >& /dev/null"

     

      write_metadata(fRef, theURL, theName, ImgPath)

      on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      close access the fRef

      return

      end try

    end repeat

    close access the fRef

     

    -- create and populate new css file (cfile) for pandoc

    set fcRef to openFileHandle((POSIX path of cssfile), true)

    init_cssfile(fcRef)

    close access the fcRef

     

    -- convert the markdown metafile into HTML

    do shell script "/usr/local/bin/pandoc -H " & (POSIX path of cssfile) & " -f markdown -t html -o " & workHTML & space & (POSIX path of metafile)

     

    tell application "Finder"

      try

      if exists file ((path to applications folder as text) & "LibreOffice.app") then

      -- command-line won't work if app is still running

      tell application "LibreOffice" to if it is running then quit

      -- Strip the trailing HFS folder (:) symbol, and quote the POSIX path to appease LO outdir arg

      set oDir to (text 1 thru -2 of outputFolder as text)'s POSIX path's quoted form

      -- convert the pandoc generated HTML file to the output ODT within the specified protokoll folder.

      set LO_CMD to "/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to odt --outdir " & oDir & space & workHTML

      do shell script LO_CMD

      end if

      on error errmsg number errnbr

      display alert "LibreOffice Error" message "The installation of LibreOffice is required for the successful conversion of the pandoc HTML into the Protokoll ODT document." & return & return & "Otherwise, LibreOffice did not write the Protokoll file for some reason." as critical giving up after 10

      my error_handler(errnbr, errmsg)

      cleanup()

      set fileCnt to (0 as integer)

      return

      end try

     

      if exists file (outputFolder & ODT_File) then

      set endMsg to "Files processed: " & (fileCnt as text) & return & return & ¬

      "The ODT file " & ODT_File & " has been written into " & outputFolder's POSIX path & " folder."

      else

      set endMsg to "Files processed: " & (fileCnt as text) & return & return & ¬

      "The ODT file " & ODT_File & " was not written for some reason, into " & outputFolder & " folder."

      set fileCnt to (0 as integer)

      return

      end if

     

      display dialog endMsg

     

      set fileCnt to (0 as integer)

     

      -- open the Protokoll ODT file in this application (if it exists)

      if exists file (outputFolder & ODT_File) then

      tell application "LibreOffice"

      activate

      open (outputFolder & ODT_File)

      end tell

      end if

     

      (*

    -- no NeoOffice installed to test

    if exists file ((path to applications folder as text) & "NeoOffice.app") ¬

    and file (outputFolder & ODT_File) then

      tell application "NeoOffice"

      activate

      open (outputFolder & ODT_File)

      end tell

    end if

    *)

    end tell

    cleanup()

    return -- end of main script

     

    -- only handlers below this point

     

    on no_whitespace(astring)

      -- convert whitespace to underscores (Leerzeichen zu Unterstrichen).

      -- uses Objective-C instead of ten lines of AppleScript in another handler

      set aStr to current application's NSString's alloc()'s initWithString:astring

      set aStr2 to (aStr's stringByReplacingOccurrencesOfString:" " withString:"_") as «class utf8»

      return aStr2

    end no_whitespace

     

    on openFileHandle(filePath, overwrite)

      -- create the filehandles enabling the writing of the metadata and css files

      try

      -- set the filePath to the filePath as Unicode text

      set the fp to open for access filePath with write permission

      if overwrite is true then set eof of the fp to 0 -- truncate file

      on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      return false

      end try

     

      return fp

     

    end openFileHandle

     

    on init_cssfile(fh)

      -- this CSS will be provided to pandoc on its command-line with the -H flag to

      -- alter the flow in the HTML to horizontal thumbnail wrap if viewed in a browser.

      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

     

      return

    end init_cssfile

     

    on write_metadata(fh, aURL, aName, theImgPath)

      -- write base64 encoded thumbnail images to markdown metadata file with associated file links.

      -- these thumbnails will be embedded in the ODT document with links to the files in the

      -- protocol folder.

      set encoded64 to do shell script "/usr/bin/base64 " & theImgPath's quoted form

      set imgBase64 to dataURI & encoded64

      write "[![](" & contents of imgBase64 & space & "\"" & aName & "\")](" & ¬

      aURL & ")" & linefeed & linefeed to fh starting at eof as «class utf8»

      return

     

    end write_metadata

     

    on cleanup()

      -- clean up all the temporary stuff that generated the protokoll ODT_File.

      tell application "Finder"

     

      try

      if exists folder thumbpath then

      delete (files of folder thumbpath)

      delete thumbpath

      end if

      set theHTML to (LocalPath & HTML_File) as text

      log theHTML

      if exists theHTML then delete file theHTML

      if exists metafile then delete file metafile

      if exists cssfile then delete file cssfile

      empty the trash

      on error errmsg number errnbr

      if errnbr = -128 then return

      my error_handler(errnbr, errmsg)

      end try

      end tell

      return

     

    end cleanup

     

    on error_handler(nbr, msg)

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

    end error_handler

  • by kaiuweheinz,

    kaiuweheinz kaiuweheinz Oct 15, 2016 6:14 AM in response to VikingOSX
    Level 1 (8 points)
    Mac OS X
    Oct 15, 2016 6:14 AM in response to VikingOSX

    Dear VikingOSX and Hiroto,

     

    I keep bragging about you two in front of students and parents (we had parents' nights this week) who gratefully profit from the protocols. Hiroto, it works like a charm - thank you for refining the process who works flawlessly and leaves no wishes. I just wonder how it manages to activate LibreOffice and has it work in the background without even opening it - amazing!

     

    VikingOSX, your beautiful workaround only works if a pdf-file is included in the selection and only then a thumbnail appears in out.odt - the one of the selected pdf-file.  This made me think: the parents/ absent student will probably only be able to deal with pdf-files (I cannot expect them to have openOffice/ LibreOffice or NeoOffice installed on their machines), so including exclusively pdf-files would probably be the best option for using your all-in-one-Script. I would need, though, to write a script that opens the selected file in NeoOffice and converts it into pdf (this program-implemented process by default saves the pdf-output in the same folder). For a single .odt-/ .ods-/ and graphic- file this is no problem. For a selection of files however I haven't found a solution yet, let alone a mixed selection of .odt-/ and jpeg-files. And what about powerpoint or keynote files which I need to include for myself for the lesson but which will probably not be of much help without my didactical guidance -so no need to convert them (wich wouldn't be possible/useful anyway).

     

    Thank you for the amazing work and time investment you two made!! Your Scripts wear your names accompanied by the date of the latest version and will always remind me and the students of the international/intercontinental collaboration/exchange we had! God bless you!

     

    “Then the righteous will answer him, ‘Lord, when did we see you hungry and feed you, or thirsty and give you something to drink? When did we see you a stranger and invite you in, or needing clothes and clothe you? The King will reply, ‘Truly I tell you, whatever you did for one of the least of these brothers and sisters of mine, you did for me.’ (Gospel of Matthew 25, verse 37)

     

    Thank you,

    Kai-Uwe Heinz

  • by VikingOSX,

    VikingOSX VikingOSX Oct 15, 2016 8:13 AM in response to kaiuweheinz
    Level 7 (21,421 points)
    Mac OS X
    Oct 15, 2016 8:13 AM in response to kaiuweheinz

    Kai-Uwe,

     

    My solution is based on Finder document preselection. Whether it is a PDF, Powerpoint, Excel, or Keynote document is irrelevant, because a final Protokol out.odt document is still produced with functional hyperlinks.

     

    The issue of standardizing on PDF certainly has an initial, sane allure to it. The internal soffice utility found within NeoOffice, OpenOffice, and LibreOffice can be used in a scripting loop to process files to PDF. However, only the LibreOffice implementation of soffice has more command-line versatility, and access to a larger file conversion library. Still, with that robustness, some document formats may require exportation to PDF from their respective applications for final PDF content accuracy.

     

    The soffice syntax in LibreOffice to convert whatever it can understand to PDF:

     

    --convert-to pdf:writer_pdf_Export, or simply --convert-to pdf

     

    Any file format that LibreOffice understands can be fed to this, and converted to PDF. This includes Keynote v5 documents, Powerpoint, and several graphics formats including SVG. The soffice utility accepts different files on the command-line, and will process them in order.

     

    Good luck.

  • by Hiroto,

    Hiroto Hiroto Oct 16, 2016 3:19 AM in response to kaiuweheinz
    Level 5 (7,348 points)
    Oct 16, 2016 3:19 AM in response to kaiuweheinz

    Hello Kai-Uwe Heinz,

     

    It's my pleasure! I'm so glad and relieved to know it serves you and your students well at last.

     

    My answering question here is like solving puzzle and solution is its own reward. If it helps good people, I'm as happy as can be.

     

    Best wishes from Japan,

    Hiroto

  • by Hiroto,

    Hiroto Hiroto Oct 16, 2016 3:21 AM in response to VikingOSX
    Level 5 (7,348 points)
    Oct 16, 2016 3:21 AM in response to VikingOSX

    Hello VikingOSX,

     

    Although I cannot test your script in my outdated environment, I think you're filtering out html and odt files in (copies of) Finder selection by the following line:

     

    set mediaList to (every item in entire contents of folder (outputFolder as alias) whose name extension is not in {"html", "odt"}) as alias list
    

     

     

    And this is the reason why Kai-Uwe Heinz cannot get the expected output when he has odt (and html) files in selection. (E.g., when he selects odt, pdf and html files in Finder, your lastest script will process pdf files only)

     

    All the best,

    Hiroto

  • by VikingOSX,

    VikingOSX VikingOSX Oct 16, 2016 5:31 AM in response to Hiroto
    Level 7 (21,421 points)
    Mac OS X
    Oct 16, 2016 5:31 AM in response to Hiroto

    Hiroto,

     

    Thanks for the mediaList advice. Scratching my head as to the original logic there.

     

    Hyperlinked HTML and ODT icons are now appearing in the final ODT document. LibreOffice will honor the hyperlink on the HTML and open it in a browser. Apparently, LibreOffice knows it already has an ODT document open, and will not honor the hyperlink on a contained ODT icon. That may be an issue for Kai-Uwe, and not one that I can fix.

     

    All the best to you...

     

    /viking

  • by VikingOSX,

    VikingOSX VikingOSX Oct 16, 2016 5:50 AM in response to kaiuweheinz
    Level 7 (21,421 points)
    Mac OS X
    Oct 16, 2016 5:50 AM in response to kaiuweheinz

    Kai-uwe,

     

    Here is the version 3.1 script update. It now allows you to include HTML and ODT documents in the Finder preselection. These now appear as hyperlinked icons in the final out.odt document. LibreOffice will not honor the hyperlink on the ODT icon — from within the ODT containing the icons. This is not something that I can fix.

     

    Code:

     

    -- protokoll3.applescript

    --

    -- Select the Finder files for processing. The script will then produce an

    -- ODT document in the selection choice location of the Protokoll folder,

    -- with the respective embedded file thumbnails, and their resolved file

    -- hyperlinks. Note: Protokoll name can be entered with spaces in its name,

    -- but script will demote spaces to underscores (Unterstrichen).

    --

    -- Revision: 3.1 • Now accepts HTML and ODT documents from Finder selection

    --                        Note: Hyperlinks on ODT icons do not work within LibreOffice from

    --                        opened ODT document

    --

    --                     • Rewritten for prompted protokoll folder creation on local, and ejectable

    --                       media

    --                     • Protokoll name

    --                        - UTF8. Spaces changed to underscores via Objective-C

    --                        - Simplifies coding with non-whitespace folder names

    --                     • More file checking and error handling

    --                     • Added a cleanup() handler before and after script execution

    --                        - does not remove any Protocol folder or contents

    --                        - removes temporary files and thumbnail folder from Desktop

    --                     • Will quit LibreOffice if it is running before attempting to

    --                        generate ODT document from HTML on command-line.

    --

    -- Requires:      • Pandoc 1.15 or later (tested with 1.17.2)

    --                     • LibreOffice v5+ (tested with v5.2.2.2)

    --                     • AppleScript 2.4+ (Yosemite or later, for direct Objective-C use)

    --                     • Script tested on El Capitan 10.11.6, and macOS Sierra 10.12.0

    --

    -- Optional:     • NeoOffice Viewer

    --                     • Ejectable thumb drive, or SD?C media card

    -- VikingOSX, October 16, 2016, Apple Support Community

     

    property VolumeName : missing value

    property ProtokollFolder : missing value

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

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

    property ODT_File : "out.odt"

    property HTML_File : "out.html"

    property workHTML : POSIX path of ((path to desktop as text) & HTML_File)

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

    property cssfile : (path to desktop as Unicode text) & "custom.css"

    property dataURI : "data:image/png;base64,"

    property fileCnt : 0 as integer

    property msg : "Please enter protokoll name for output folder." & return & ¬

      "Spaces in names will be changed to '_'."

     

    use scripting additions

    use framework "Foundation"

    use AppleScript version "2.4" -- Yosemite or later (for direct Objective-C usage)

     

    try

      cleanup() -- remove all temporary files from last run that may exist

      -- stop script if user has not preselected Finder files before script is run.

      tell application "Finder"

      -- Finder selected documents are made into a file list

      set inPath to the selection as alias list

      if inPath = {} then

      display alert "Finder" message "You must select Finder items before executing this script." as critical giving up after 10

      return

      end if

      end tell

     

      -- get name of output protocol folder

      set ProtokollFolder to text returned of (display dialog msg default answer ¬

      "" with title "Protokoll") as «class utf8»

     

      if ProtokollFolder contains space then

      -- change spaces in protokoll name to underscore

      set ProtokollFolder to no_whitespace(ProtokollFolder)

      end if

     

      set MediaChoice to (choose from list {"Mounted", "Desktop"} with title "Protokoll Location" with prompt "Choose local drive, or mounted media." default items {"Desktop"} without multiple selections allowed and empty selection allowed)

      if MediaChoice is false then error number -128

     

      -- Request location to make named folder after the protocol provided

      tell application "Finder"

      set outputFolder to (LocalPath & ProtokollFolder) as text

      if MediaChoice contains "Desktop" and ¬

      not (exists folder outputFolder) then

      set outputFolder to (make new folder at LocalPath with properties {name:ProtokollFolder}) as text

      else if MediaChoice contains "Mounted" then

      get name of (every disk whose ejectable is true and its name is not in {"Time Machine", "TIME MACHINE"}) as list

      set VolumeName to (choose from list (result) with title "Mounted Volumes" default items "" without multiple selections allowed and empty selection allowed) as «class utf8»

      if VolumeName is false then return -- cancel button

     

      if VolumeName contains space then

      end if

      set VolumeName to ("/Volumes/" & VolumeName & "/") as text

      if not (exists outputFolder) then

      set outputFolder to (make new folder at (VolumeName as POSIX file) with properties {name:ProtokollFolder}) as text

      end if

      end if

      end tell

    on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      return

    end try

     

    tell application "Finder"

      -- place Finder selected files in the output Protocol folder

      repeat with afile in inPath

      duplicate afile to folder (outputFolder as alias) with replacing

      end repeat

      -- make a list of the files in the protokoll folder

      set mediaList to (every item in entire contents of folder (outputFolder as alias)) as alias list

     

      if mediaList = {} then

      display alert "Finder" message "No files found in mounted media folder." as critical giving up after 5

      return

      end if

     

      set fileCnt to (count of mediaList) as integer

      make new folder at LocalPath with properties {name:"Thumbs"}

     

    end tell

     

    -- get metadata file handle for writing

    set fRef to openFileHandle((POSIX path of metafile), true)

    if fRef is false then return

     

    -- generate the thumbnails from each file, and write to the metafile.

    repeat with afile in mediaList

      tell application "Finder"

      set theURL to (afile's URL) as «class utf8»

      set theName to (afile's name) as «class utf8»

      set ImgPath to ""

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

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

      end tell

      try

      -- use the QuickLook manager to generate a 200 pixel icon into the thumbnail folder.

      do shell script "/usr/bin/qlmanage -ti -s 200 " & inFile & " -o " & POSIX path of (thumbpath as alias) & " >& /dev/null"

     

      write_metadata(fRef, theURL, theName, ImgPath)

      on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      close access the fRef

      return

      end try

    end repeat

    close access the fRef

     

    -- create and populate new css file (cfile) for pandoc

    set fcRef to openFileHandle((POSIX path of cssfile), true)

    init_cssfile(fcRef)

    close access the fcRef

     

    -- convert the markdown metafile into HTML

    do shell script "/usr/local/bin/pandoc -H " & (POSIX path of cssfile) & " -f markdown -t html -o " & workHTML & space & (POSIX path of metafile)

     

    tell application "Finder"

      try

      if exists file ((path to applications folder as text) & "LibreOffice.app") then

      -- command-line won't work if app is still running

      tell application "LibreOffice" to if it is running then quit

      -- Strip the trailing HFS folder (:) symbol, and quote the POSIX path to appease LO outdir arg

      set oDir to (text 1 thru -2 of outputFolder as text)'s POSIX path's quoted form

      -- convert the pandoc generated HTML file to the output ODT within the specified protokoll folder.

      set LO_CMD to "/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to odt --outdir " & oDir & space & workHTML

      do shell script LO_CMD

      end if

      on error errmsg number errnbr

      display alert "LibreOffice Error" message "The installation of LibreOffice is required for the successful conversion of the pandoc HTML into the Protokoll ODT document." & return & return & "Otherwise, LibreOffice did not write the Protokoll file for some reason." as critical giving up after 10

      my error_handler(errnbr, errmsg)

      cleanup()

      set fileCnt to (0 as integer)

      return

      end try

     

      if exists file (outputFolder & ODT_File) then

      set endMsg to "Files processed: " & (fileCnt as text) & return & return & ¬

      "The ODT file " & ODT_File & " has been written into " & outputFolder's POSIX path & " folder."

      else

      set endMsg to "Files processed: " & (fileCnt as text) & return & return & ¬

      "The ODT file " & ODT_File & " was not written for some reason, into " & outputFolder & " folder."

      set fileCnt to (0 as integer)

      return

      end if

     

      display dialog endMsg

     

      set fileCnt to (0 as integer)

     

      -- open the Protokol ODT file in this application (if it exists)

      if exists file (outputFolder & ODT_File) then

      tell application "LibreOffice"

      activate

      open (outputFolder & ODT_File)

      end tell

      end if

     

      (*

    -- no NeoOffice installed to test

    if exists file ((path to applications folder as text) & "NeoOffice.app") ¬

    and file (outputFolder & ODT_File) then

      tell application "NeoOffice"

      activate

      open (outputFolder & ODT_File)

      end tell

    end if

    *)

    end tell

    cleanup()

    return -- end of main script

     

    -- only handlers below this point

     

    on no_whitespace(astring)

      -- convert whitespace to underscores (Leerzeichen zu Unterstrichen).

      -- uses Objective-C instead of ten lines of AppleScript in another handler

      set aStr to current application's NSString's alloc()'s initWithString:astring

      set aStr2 to (aStr's stringByReplacingOccurrencesOfString:" " withString:"_") as «class utf8»

      return aStr2

    end no_whitespace

     

    on openFileHandle(filePath, overwrite)

      -- create the filehandles enabling the writing of the metadata and css files

      try

      -- set the filePath to the filePath as Unicode text

      set the fp to open for access filePath with write permission

      if overwrite is true then set eof of the fp to 0 -- truncate file

      on error errmsg number errnbr

      my error_handler(errnbr, errmsg)

      return false

      end try

     

      return fp

     

    end openFileHandle

     

    on init_cssfile(fh)

      -- this CSS will be provided to pandoc on its command-line with the -H flag to

      -- alter the flow in the HTML to horizontal thumbnail wrap if viewed in a browser.

      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

     

      return

    end init_cssfile

     

    on write_metadata(fh, aURL, aName, theImgPath)

      -- write base64 encoded thumbnail images to markdown metadata file with associated file links.

      -- these thumbnails will be embedded in the ODT document with links to the files in the

      -- protocol folder.

      set encoded64 to do shell script "/usr/bin/base64 " & theImgPath's quoted form

      set imgBase64 to dataURI & encoded64

      write "[![](" & contents of imgBase64 & space & "\"" & aName & "\")](" & ¬

      aURL & ")" & linefeed & linefeed to fh starting at eof as «class utf8»

      return

     

    end write_metadata

     

    on cleanup()

      -- clean up all the temporary stuff that generated the protokoll ODT_File.

      tell application "Finder"

     

      try

      if exists folder thumbpath then

      delete (files of folder thumbpath)

      delete thumbpath

      end if

      set theHTML to (LocalPath & HTML_File) as text

      log theHTML

      if exists theHTML then delete file theHTML

      if exists metafile then delete file metafile

      if exists cssfile then delete file cssfile

      empty the trash

      on error errmsg number errnbr

      if errnbr = -128 then return

      my error_handler(errnbr, errmsg)

      end try

      end tell

      return

     

    end cleanup

     

    on error_handler(nbr, msg)

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

    end error_handler

first Previous Page 5 of 5