Mac: print pdf to apple note - macos

I am trying to move from using Evernote to apple note.
One function that I am missing is from the print menu on a mac to be able to save the PDF to a new note.
I figured that I can create a print_plugin from Automator. I am trying to have an apple script handling the creation of a new note. Googling and using my limited knowledge of apple script I figured that I need to do something like
property accountName : "iCloud"
on run {input, parameters}
tell application "Preview"
open input
end tell
tell application "Notes"
activate
set theFolder to choose from list ((name of folders of account accountName) as list)
if theFolder is not false then
set newNote to make new note in folder (item 1 of theFolder) of account accountName
if input is not {} then
tell newNote
make new attachment with input
show
end tell
end if
end if
end tell
end run
The preview part of the code is to see if the pdf is passed correctly. Preview can indeed open the pdf correctly.
When I run the script I get the following error
The action “Run AppleScript” encountered an error: “Notes got an error: Can’t make show id "x-coredata://726D28C7-1847-43DA-9A8D-A9A1B379DF6F/ICNote/p6780" into type specifier.”
Notes got an error: Can’t make show id "x-coredata://726D28C7-1847-43DA-9A8D-A9A1B379DF6F/ICNote/p6780" into type specifier
If I remove the line
make new attachment with input
the script runs without error (but clearly no attachment is created).
Does anyone have a solution?
Best,
E

Ok, after a lot of meddling I found this sort of solution:
property accountName : "iCloud"
on run {input, parameters}
tell application "Notes"
open input
end tell
end run
When saved as a print plugin, it creates a new note with the title of the webpage and the pdf attached. It creates it in the "Notes" folder, which works fine for me.
Best,
E

Related

AppleScript running Atom text-editor every time script is run instead of when if statement is true

I'm new to AppleScript and I'm trying to make a simple AppleScript that will pop-up a dialog asking for user input and based on the input it will open one application or another. To do this I've used:
tell application "atom"
activate
end tell
One of the applications is stored on an external hard drive called Tardis. The problem with my code below is that it automatically opens Atom every time I run the script AND the dialog window at the same time. I only want it to open Atom when the input a is given. Thanks for the help! Code is below:
on run {input, parameters}
set inputText to text returned of (display dialog "Options: p - a - j" default answer "")
set p to "p"
set a to "a"
set j to "j"
if (inputText = p) then
tell application "Terminal"
if it is running then
do script "cd Python_Workspace; source ~/tensorflow/bin/activate"
end if
activate
end tell
tell application "Finder" to open POSIX file "/Volumes/Tardis/EXTRA Applications/Sublime Text.app"
end if
if (inputText = a) then
tell application "Atom"
activate
end tell
tell application "Terminal"
if it is running then
do script ""
end if
activate
end tell
end if
if (inputText = j) then
tell application "Terminal"
if it is running then
do script "cd /Java_Workspace"
end if
activate
end tell
tell application "IntelliJ IDEA CE"
activate
end tell
end if
return input
end run
So some applications that support Applescript do - or did, require the application to launch to compile the Applescript at all (if I remember correctly these are applications that support Applescript poorly, which I'm betting is Atom).
This may not be exactly what's happening, especially since if you are launching a compiled Applescript, but maybe you are running a .applescript (text) file.
What I would do, instead of tell application "Atom" to activate (a one line version of what you have), instead: activate application "Atom".
If that does work for you, activate vs tell here's what's going on:
Applescript notices you're talking about Atom. It needs a dictionary to translate your typing into... events.
Applecript can't find the dictionary, so it launches the application, hoping that it can get more information from it when it launches
Atom launches, Applescript realizes that every application supports activate.
Where activate application you're not trying to do anything Atom specific... just activate an app. No dictionary lookup required.
(It's also worth noting that I've been doing this a very long time. I could actually be quoting you information that's a decade or more out of date, or "new" in 10.4 - not 10.14, but 10.4)

How to script an application using AppleScript, but only if installed?

A few of my scripts notify using Growl, but I'm trying to run them now on a machine that doesn't have Growl installed.
This line
tell application "Growl"
notify with name "Script" title "Script" description NotifyText application name "Script" with sticky
-- _
end tell
Gives this compiler error at the character underlined:
Expected “given”, “into”, “with”, “without”, other parameter name, etc. but found “"”.
How can an AppleScript optionally use an application?
To my knowledge Script Editor does not have a conditional compile directive and if an application is not installed then it will error out on compile. In some cases, Script Editor can be faked out into compiling by creating a dummy app in the name of the missing application but this has limited application and success and really depends on the code written for the missing application.
As to writing code to conditionally use a target application if it's installed, one needs to test for its existence and wrap the target applications code in a if statement block. In the following example AppleScript code, I have a test video file in my Movies folder that I want to play in VLC if it's installed, and if not then play in QuickTime Player, e.g.:
set thisVideo to (path to movies folder as string) & "test.mp4"
try
tell application "Finder" to get application file id "org.videolan.vlc"
set appExists to true
on error
set appExists to false
end try
if appExists then
set theApp to "VLC"
tell application theApp to open thisVideo
else
tell application "QuickTime Player"
open thisVideo
play document 1
end tell
end if
Now I do not have Growl installed but the example AppleScript code shown above should work for it as well. From a Mac that has it installed, you just need to ascertain its bundle identifier property while the target application is running and use it in place of what's in the example code, i.e.:
tell application "System Events" to get bundle identifier of application process "Growl"
Note: The example AppleScript code is just that and does not employ any other error handling then what's shown and is meant only to show one of many ways to accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.

Make apple script code work on any laptop (not need the user's name)

I have a simple code of deleting a file and making a folder. But how do I make it run on any laptop because everyone's user name is different. I know it has something to do with "~" but i'm not sure where.
The code below works properly but how do I make sure it can run on any mac:
try
tell application "Finder"
delete ((POSIX file "/Users/myname/Desktop/thefile.txt"))
make new folder at (POSIX file ("/Users/myname/Desktop")) with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try
The syntax independent of the user name is much simpler since the desktop folder of the current user is the root folder of the Finder
try
tell application "Finder"
delete file "thefile.txt"
make new folder at desktop with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try
You can also
set username to ""
tell application "System Events"
set username to full name of current user
end tell
try
tell application "Finder"
delete ((POSIX file "/Users/username/Desktop/thefile.txt"))
make new folder at (POSIX file ("/Users/username/Desktop")) with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try

Creating an automator service to create a new document in the current directory

so I'm trying to create a service that will be located in the contextual menu of the Finder and that would allow to create a new document in the current directory.
I've been doing that using Automator:
Sorry everything's in French ^^
Anyway here's the AppleScript that I'm using to retrieve the current working directory:
on run {input, parameters}
tell application "Finder"
set pwdAlias to insertion location as alias
if not (exists folder pwdAlias) then
set pwdAlias to (container of pwdAlias) as alias
end if
end tell
set pwd to POSIX path of pwdAlias
return pwd
end run
Then I'm setting this value to a variable, then creating a new text document using the variable as the path for the document and finally I'm using the command Reveal in Finder to show the created document.
Everything's is working fine except that the script seems to always be late!
What I mean is that when I open a new Finder window and select my service, it is systematically creating the document on the previous window as shown below:
But then if I try a second time, the document is being created properly at the expected location:
And this is very systematic it happens every time!!
Sorry if I'm not very clear, it is not so easy to explain!
Well otherwise, I'm running Mountain Lion and here's the Automator project attached: create_new_document
To add the service just unzip and put the file under ~/Library/Services/
Hope to get some answers but I fear that this is just an Automator bug!
Try this
Depending on what you want to be clicking.
Set the Services selected to: 'folders'
or files or folders. in 'Finder.app'
Get first Finder Window path Action
You can download the Get first Finder Window path Action from my blog post here
The download is at the bottom of the post.
The Action gets the posix path of the frontmost finder window.
Since you are clicking on a folder in a window. that window will be the one returned.
Set Value of Variable
Get Specified Text
The next action 'New Text File' needs some input. If it does not get any, no file will be created. You can leave the text field blank. Just having the action in place works.
New Text File
Drag the Variable 'path' or what ever you named it on to the Where: drop down menu.
you can click the double blue lines at the bottom of the Automator window to toggle the workflow Variable List
Save your service. And try it.
(It may take a short while to show up in the contextual Menu.)
It's an open bug in 10.7 and 10.8
Use this Workaround
on run {input, parameters}
activate application "System Events"
activate application "Finder"
tell application "Finder"
set pwdAlias to insertion location as alias
set pwdAlias to (container of pwdAlias) as alias
end tell
return POSIX path of pwdAlias
end run

Send all files on Desktop to Evernote then delete

Good morning,
I am trying to write an AppleScript that I can run that will send all the files on my desktop to Evernote, and then delete the files. My code to date is:
on run {input}
tell application "Finder"
select every file of desktop
end tell
tell application "Evernote"
repeat with SelectedFile in input
try
create note from file SelectedFile notebook "Auto Import"
end try
end repeat
end tell
tell application "Finder"
delete every file of desktop
end tell
end run
If I run this then the first and last 'tell' work fine (ie. the script highlights then deletes all the files on the desktop), but the middle 'tell' doesn't do anything.
However, if I manually highlight all the files on the desktop and then run just the middle 'tell' then it imports fine - each item into a separate note as intended.
As you can tell, I am new to AppleScript - I suspect I need to put the selected files in an array of some sort, but can't figure it out. Help!
Many thanks
Rich
Your code fails because there is no relation between your input variable and the selection of files via Finder – which means that your list is empty, and Evernote is not processing anything at all. You have obfuscated the problem by wrapping the Evernote import command in a try block without any error processing, which means all errors just go unnoticed (to avoid this kind of problem, it is good practice to always log the error message in an on error clause, if nothing else).
Also, you don’t actually need to select files on the Desktop via AppleScript to process them. The following code will grab all visible files (excluding pseudo-files like packages / apps):
tell application "System Events"
set desktopFiles to every disk item of (desktop folder of user domain) whose visible is true and class is file
end tell
Pass the list you retrieved that way to Evernote for processing:
repeat with aFile in desktopFiles as list
try
tell application "Evernote" to create note from file (aFile as alias) notebook "Auto Import"
tell application "System Events" to delete aFile
on error errorMessage
log errorMessage
end try
end repeat
and you are good to go.
Note that by judiciously placing the deletion command (right after the import command, inside the try block, inside the loop over all files), you make sure it is only called if Evernote does not error on import while avoiding having to iterate over the files several times.
A final note: you don’t have to use the block syntax for tell statements if there is only one command to execute – using tell <target> to <command> is easier and will keep you out of nested context hell.
Thanks #adayzone for corrections on list handling and alias coercion
Try
tell application "System Events" to set xxx to get every file of (desktop folder of user domain) whose visible is true
repeat with i from 1 to count of xxx
set SelectedFile to item i of xxx as alias
try
tell application "Evernote" to create note from file SelectedFile notebook "Auto Import"
tell application "Finder" to delete SelectedFile
end try
end repeat
Thanks #fanaugen

Resources