Create apple notes with AppleScript - applescript

I get the text from the file and create a note, but the text is added to the notes without hyphenation, in one line! How to make all hyphenations be copied?
My code:
set x to read POSIX file "/files/mytext.txt"
tell application "Notes"
tell account "iCloud"
make new note at folder "Blogs" with properties {name:"My Blogs", body:x}
end tell
end tell
Thanks!

I discovered that the "Notes" application is actually using HTML format (it said so in the AppleScript Dictionary for that application), and HTML does not correctly format newlines; so that means that all of the newlines need to be replaced with <br> using the shell command awk. Reading the file using awk instead of AppleScript itself also seems to solve the hyphenation problem.
Here is the fixed command:
set x to (do shell script "awk '{printf \"%s\\<br>\", $0}' '/files/mytext.txt'")
tell application "Notes"
tell account "iCloud"
make new note at folder "Blogs" with properties {name:"My Blogs", body:x}
end tell
end tell

Related

Open file in Preview

I struggle with a basic file operation in Apple Script. A pdf is (successfully) written in the script like this:
theDocument's writeToURL:targetFile
Now I want to open the file in Preview like this:
tell application "Preview"
activate
open targetFile
end tell
I also tried this:
tell application "Preview"
activate
open POSIX path of targetFile
end tell
to no avail. I somehow seem to lack an understanding how to get the path to a given file.
What am I missing?
Your code doesn’t show what targetFile is, but assuming from the first line it’s an NSURL you need to convert it to an AppleScript file type before passing it to Preview’s open command:
set targetFile to targetFile's |path|() as string as POSIX file
AppleScript’s Apple event bridge doesn’t accept ObjC classes, only native AS types.

Run Automator Workflow in background

Trying to make Automator workflow that uses "Convert to TXT Document" for pdf to txt convertion. But while running, Abbyy FineReader window becomes active. Is it possible to run in in silent mode or with minimized window?
This AppleScript works for me using the latest version of Sierra. Tested on my system, it did not bring Abbyy FineReader to the foreground.
set thePDF to (choose file)
tell application "FineReader"
set resultFile to export to txt thePDF ¬
from file thePDF
end tell
Your new text file should appear in the same directory as your original PDF
I'm not using Automator so I do not know which method you are going to use to pass the PDF file to this AppleScript. For testing purposes, I used to the “choose file” command. If you are using Automator to pass the PDF file that you specified in a previous Automator action, you can just remove the “choose file” command from the code. Anyway, all you need to do is add a “run AppleScript” command in your Automator workflow.
If you are going to remove the “choose file” command, you'll need to re-define the value for the variable thePDF
NOTE FineReader actually has an extensive AppleScript dictionary. My answer included a minimal version of many other options for the exporting as text. Here is a full version example of options
tell application "FineReader"
set resultFile to export to txt directParamFile ¬
from file fromFileFile ¬
ocr languages enum ocrLanguagesEnumLanguageListType ¬
saving type savingTypeSaveSettingsEnum ¬
retain layout retainLayoutTxtLayout ¬
keep page numbers headers and footers keepPageNumbersHeadersAndFootersBoolean ¬
keep line breaks and hyphenation keepLineBreaksAndHyphenationBoolean ¬
insert page break character as page separator insertPageBreakCharacterAsPageSeparatorBoolean ¬
use blank lines useBlankLinesBoolean ¬
encoding encodingEncodingEnum
end tell
I decided not to use FineReader applet. Instead I migrate to stack: tesseract + ImageMagick + gs.
If anybody is interested, I attach my sollution below.
Automator shell script
export PATH=/usr/local/bin:$PATH
/usr/local/bin/convert -density 300 "$#" -depth 8 -strip -background white -alpha off image.tiff
/usr/local/bin/tesseract -l rus image.tiff ~/Desktop/OCR
rm image.tiff
And the
Automator workflow
You could try this applescript in the script editor, replacing the file paths to your files. I don't have the program installed, so I haven't tested it. If it doesn't work, maybe it's something you can build on to get the result you're after.
tell application "FineReader" activate
tell application "System Events" set visible of process "FineReader" to false
tell application "FineReader"
export to txt "/Path/to/filename/File_to_OCR.pdf" from file "/Path/to/filename/File_to_OCR.pdf"
end tell

Applescript - save argument to text file

I need to write a simple applescript that will be opened with an argument. That argument will be a file path. I need it to save this file path to a text file. I want to avoid using shell script.
I'm finding this surprisingly difficult, despite having done it in .sh and .bat already
Thanks very much!
Adam
//// the current code which is not working is below. the code begins with "on run" and ends with "end run" but for some reason this isn't being highlighted as code
on run argv
set this_PATH to (POSIX file argv)
tell application "TextEdit"
activate
make new document
set text of document 1 to this_PATH as text
save document 1 in "/Users/adamparkinson/Desktop/date.txt"
end tell
end run
I made this work:
#!/usr/bin/osascript
on run argv
tell application "TextEdit"
activate
make new document
tell document 1
set its text to (item 1 of argv as text)
save in posix file (item 1 of argv as text )
end tell
end tell
end run
I called it like this:
./SavePath.sh /Users/Me/Desktop/junk/Newstuff.txt (Of course I used chmod u+x SavePath.sh to make it executable.)
If this works for you, then please check of my answer as answered. :)
with applescript it is very easy to create application Drag and Drop without any script shell below a version Drag and Drop of your script
on open draggedItems
set this_PATH to quoted form of POSIX path of draggedItems
repeat with currentItem in draggedItems
tell application "TextEdit"
activate
set doc to open POSIX file "/Users/adamparkinson/Desktop/date.txt"
make new word at end of text of doc with data (this_PATH as text) & return
end tell
end repeat
end open

OS X 10.9 Applescript changes: using the `move` command in the "System Events" context to move a file

I went to run an old script and it broke after the 10.9 update. I used to move files with system events with the following code.
set Somefilepath to "Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt"
set somefolderpath to "Design_005_HD:Users:Design_005:Desktop:End:"
tell application "System Events"
move file (Somefilepath) to folder (somefolderpath)
end tell
Now it gives me the following error.
error "System Events got an error: Can’t make file
\"Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt\" into
type integer." number -1700 from file
"Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt" to integer
I know I can swap it out and use finder but I rather not use it. What changed that is no longer works?
Update 4/2/14
I have tried this in every way of giving the file/folder location to system events and it doesn't work. I am glad it is just not me who cannot get it to work. I will update this post if I find an answer or a working update is made.
Update 4/3/14
It seems this is just a bug that system events can't move files. I have reported it here http://bugreport.apple.com/ and everyone else should too.
Please do not take my code to heart, it is just where things ended up when I couldn't get it to work. I have working code for 10.8.5 and it is what is shown above minus the folder tag in the system events tell block. No idea why it works with out the folder tag but it does. Tested on multiple comps. If it isn't broken don't fuss over it. Noted it and moved on.
Update 10/20/14
For anyone interested. I have received an e-mail stating my ticket has been closed/resolved. So mavericks will for ever be broken but there might be light for Yosemite when it comes out.
In general, Applescript works with colon delimited paths (:) not slash delimited paths (/). I say in general because some applications will work with slashes but all programs will work with colons. For an example of what the colon paths look like try this code and look at the result...
set colonPath to (path to desktop as text) & "untitled folder 2:"
So first I would convert your slashes to the colon style.
Also to applescript these are just strings not paths. To make applescript understand they are paths we can do a few things. First you can add the words file or folder in front of them as appropriate. I notice in your code you are using "file" in front of the file string but you aren't using "folder" in front of the folder string. So try that. Second you can just use "alias" in front of the strings whether they're files or folders. There are other ways as well but I'll stop here. Either of those ways should work.
UPDATE: with all of the above being said, it seems System Events in 10.9 still has trouble with the move command. As such here's 2 alternatives for you. I used slash style paths since that's what you're using.
set somefilepath to POSIX file "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to POSIX file "/Users/Design_005/Desktop/End"
tell application "Finder"
move somefilepath to somefolderpath
end tell
or
set somefilepath to "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to "/Users/Design_005/Desktop/End"
do shell script "mv " & quoted form of somefilepath & space & quoted form of somefolderpath
Good luck.
You can’t do that. System Events can delete and open, but not move. Here’s a simple example in case it helps someone else find a better answer in a future OS. System Events appears to treat move differently than delete and open.
tell application "System Events"
set myFile to file "Macintosh HD:Users:velma:Desktop:Test.png"
set myFolder to folder "Macintosh HD:Users:velma:Desktop:Test"
--delete works! with both type “file/folder” and type “disk item”
--delete myFile
--delete myFolder
--open works!
open myFile
open myFolder
--move fails!
move myFile to myFolder
end tell
The error it’s returning, in this case, is “Can’t get file”, number -1728.
There appears to be bug in the move command in the "System Events" context in OX 10.9 (and possibly 10.8).
The workaround is the to use the "Finder" context instead:
Using HFS-style paths (separator is :)
set somefilepath to "Design_005_HD:Users:Design_005:Desktop:Start:TextFile.txt"
set somefolderpath to "Design_005_HD:Users:Design_005:Desktop:End:"
tell application "Finder"
move file somefilepath to folder somefolderpath
end tell
Using POSIX-style paths (separator is /) - as in the original question
set somefilepath to "/Users/Design_005/Desktop/Start/TextFile.txt"
set somefolderpath to "/Users/Design_005/Desktop/End"
# Note that we use `as POSIX file` even in the case of the *folder*
# - this works, however.
tell application "Finder"
move somefilepath as POSIX file to somefolderpath as POSIX file
end tell
Note:
as POSIX file returns a file object in both cases, but Finder still handles the move properly - it is fine to use POSIX file with both files and folders.
Note that using the prefix form of POSIX file- e.g., POSIX file "/Library", only works with a path string literal; if you try to build the path string as an expression, it breaks (in the "Finder" context, but NOT in the AppleScript context(!)): POSIX file ("/" & "Library") - by contrast, "/" & "Library" as POSIX file works (in both contexts) - go figure. To be safe, always use the postfix form: ... as POSIX file
A downside of using as POSIX file - at least as of OS X 10.9 - is that the error messages are cryptic if a file/folder doesn't exist: you'll see Finder got an error: Handler can’t handle objects of this class. and Finder got an error: AppleEvent handler failed. - both with number -10000.
(Using folder directly with a POSIX path, as in an earlier version of the question - e.g., folder "/Library" - ONLY works in a "System Events" context, and is therefore NOT an option in the "Finder" context.)
As for what changed in AppleScript OS X 10.9:
The behavior you see appears to be a bug (also see #Jerry Stratton's answer); nothing in the AppleScript release notes for 10.9 indicates a change in that area.
I now think that the problem affects OS X 10.8 as well.
I encourage you to submit a bug report to Apple at http://bugreport.apple.com, as I already have.
Sadly, handling files, folders, and aliases in AppleScript has always been a mess, with confusion stemming from classes of the same name from different dictionaries (AppleScript itself, System Events, Finder) with subtly different behavior.
A general recommendation: for file-system manipulation, use the tell application "Finder" context.
The "System Events" dictionary, in its Disk-Folder-File Suite, duplicates some of Finder's file-system manipulation functionality, but only some - a curious omission is a file copy command, for instance.

How can I batch save a bunch of AppleScript scpt files to applications?

I have a bunch of AppleScript scripts in a directory that I want to be able to build as apps without having to go into each one and manually save as application.
I've already created a "build script" that used to work, but for some reason it doesn't work anymore. So I'm able to iterate through the scripts, open them in the AppleScript Editor, create the output directory, but the save command doesn't do anything anymore. Here's the relevant part:
on makeApp(sourcedir, outputdir, curFile)
tell application "Finder" to set outName to name of curFile
tell application "AppleScript Editor"
set outNameRoot to my remove_extension(outName)
set outAppName to outNameRoot & ".app"
set outFileName to ((outputdir as string) & outAppName as string)
set scriptFile to open curFile
save scriptFile as "application" in outFileName
--display dialog outFileName
--close scriptFile
end tell
end makeApp
The line that fails is:
save scriptFile as "application" in outFileName
This was working around September. The only thing I can think of is that I've upgraded to Lion. So my question is:
Does anyone know why that would fail (silently)?
Is there another way to accomplish what I'm trying to do?
Thanks
The open command is supposed to return a document, but it returns missing value on my machine. You should be able to just save document 1, since the newly opened script will be in front. Another option would be to use osacompile - you don't get a bunch of script documents popping up that way.

Resources