• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: convert text data to PDF
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: convert text data to PDF


  • Subject: Re: convert text data to PDF
  • From: Yvan KOENIG <email@hidden>
  • Date: Thu, 17 Sep 2015 18:10:31 +0200


Le 2015/09/17 à 01:49, Shane Stanley <email@hidden> a écrit :

On 17 Sep 2015, at 3:12 AM, Yvan KOENIG <email@hidden> wrote:

I ran these instructions:
tell application "Notes"
activate
set noteBody to body of second note
end tell
set the clipboard to noteBody
clipboard info

and got :
{{Unicode text, 300}, {string, 150}, {scrap styles, 22}, {«class utf8», 150}, {«class ut16», 302}, {scrap styles, 22}}

When the clipboard contain PDF datas, writing them in a PDF file is easy.
Alas I found no way to convert the text into PDF datas.

All you have there is simple AppleScript text. You can put that into a PDF, but I suspect that what the OP wants is something that looks like what he sees in Notes -- and that's not what you're going to get. Any styling information has been thrown out.


Maybe for the first time I disagree with you.
What is stored in the variable noteBody is not raw text. When it's stored in a text file with the correct header it may be written in a html document which contain every attributes.
If I paste the clipboard filled by the posted script in a TextEdit or a Pages document, I get the styled text.
So, I hoped that there is a way to convert this styled text into PDF datas.

But happily, I was able to do the entire job with GUI Scripting. It was quite easy and it's more efficient because, as the export is done from Notes, the resulting PDF contain everything, even pictures when a note contain some of them.

Fo those which may be interested, here is the beast.

(*
Export notes from Notes to PDF files

Yvan KOENIG (VALLAURIS, France) 
2015/09/16
*)

#=====

on buildTitle(originalText)
set normalizedText to my replace(originalText, ":", "-")
return my firstChars(normalizedText, 100)
end buildTitle

#=====

on replace(originalText, fromText, toText)
set oTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to the fromText
set the item_list to every text item of originalText
set AppleScript's text item delimiters to the toText
set originalText to the item_list as string
set AppleScript's text item delimiters to oTIDs
return originalText
end replace

on firstChars(originalText, maxChars)
if length of originalText < maxChars then
return originalText
else
return text 1 thru maxChars of originalText
end if
end firstChars

(*
on horodateur(une_date) # builds yyyymmdd_hhmmss
tell une_date to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateur
*)
on horodateurAlt(une_date) # builds yyyy-mm-dd_hhmmss
tell une_date to return ((text -4 thru -1 of ((its year) + 10000 as text)) & "-" & (text -2 thru -1 of ((its month) + 100 as text)) & "-" & (text -2 thru -1 of ((its day) + 100 as text))) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horodateurAlt


on exportAsPDF(FolderPath, pdfName)


set posixFolderPath to POSIX path of FolderPath
set pdfPath to POSIX path of (FolderPath & pdfName)


tell application "System Events"
if exists disk item pdfPath then
delete disk item pdfPath
delay 0.2
end if
end tell
# Code borrowed to Nigel Garvey (http://macscripter.net/viewtopic.php?id=41654)
tell application "System Events"
tell process "Notes"
set frontmost to true
set {mt, mi} to {3, 8}


tell menu bar 1
log (get name of menu bar items) (*Apple, Notes, Fichier, Édition, Format, Présentation, Fenêtre, Aide*)
--log (get name of menu bar item mt) (*Fichier*)
tell menu bar item mt to tell menu 1
log (get name of menu items) (*Nouvelle note, Nouveau dossier, missing value, Fermer, Tout fermer, missing value, Partager, Exporter au format PDF…, missing value, Imprimer…*)
log (get name of menu item mi) (*Exporter au format PDF…*)
click menu item mi
end tell # menu bar item mt
end tell # menu bar 1


repeat until exists sheet 1 of window 1
delay 0.02
end repeat
tell sheet 1 of window 1
-- log (get position of text fields) (*910, 118, 910, 148*)
set value of text field 1 to pdfName
end tell


keystroke "g" using {command down, shift down}
repeat until exists sheet 1 of sheet 1 of window 1
delay 0.02
end repeat
tell sheet 1 of sheet 1 of window 1
set value of text field 1 to posixFolderPath
-- log (get name of buttons) (*Aller, Annuler*)
click button 1
end tell
tell sheet 1 of window 1
-- log (get name of buttons) (*Enregistrer, Nouveau dossier, Annuler*)
click button 1
end tell


end tell # process


end tell # "System Events"


end exportAsPDF

#=====

tell application "Notes"
activate
set nbNotes to count of notes
end tell
tell application "SystemUIServer"
display dialog "This is the export utility for Notes.app.
" & "Exactly " & nbNotes & " notes are stored in the application. " & "Each one of them will be exported as a simple HTML file stored in a folder of your choice." with title "Notes Export" buttons {"Cancel", "Proceed"} cancel button 1 default button 2
set exportFolder to choose folder
end tell

tell application "System Events" to tell process "Notes"
set frontmost to true
tell window 1
-- log (get class of UI elements) (*splitter group, button, button, button, static text*)
tell first splitter group
-- log (get class of UI elements) (*scroll area, splitter, group, splitter, group*)
-- log (get position of groups) (*717, 135, 959, 135*)
tell first group
-- log (get class of UI elements) (*text field, scroll area*)
-- log (get value of first text field) (**)
tell first scroll area
-- log (get class of UI elements) (*table, scroll bar*)
tell first table
-- log (get class of UI elements) (*row, row, row, row, row, row, row, row, row, row, row, column*)
repeat with i from 1 to count row
tell application "Notes" to tell note i # Grab parameters to build the PDF name
set noteName to name
set noteDate to my horodateurAlt(creation date)
end tell
set value of attribute "AXSelected" of row i to true # select the note to export
set noteTitle to my buildTitle(noteName)
set theName to noteDate & " " & noteTitle & ".pdf"


my exportAsPDF(exportFolder as text, theName) # Export the note


end repeat
end tell # 1st table
end tell # 1st scroll area
end tell # 1st group
end tell # 1st splitter group
end tell # window 1
end tell # System Events…



Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) jeudi 17 septembre 2015 18:09:57


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: convert text data to PDF
      • From: Shane Stanley <email@hidden>
References: 
 >convert text data to PDF (From: Yvan KOENIG <email@hidden>)
 >Re: convert text data to PDF (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: List Files Sorted by Last-Added Date
  • Next by Date: Contacts accounts
  • Previous by thread: Re: convert text data to PDF
  • Next by thread: Re: convert text data to PDF
  • Index(es):
    • Date
    • Thread