AppleScript can't find image in Users Library folder - applescript

I'm new to AppleScript and i'm trying to figure this out.
I'm trying to add an attachment for an email and I can get the image with
tell msg to make new attachment with properties {file name:"/Users/jarrod/Library/img/image.png"} at after the last paragraph
but I want the script to be dynamic and work on other computers so I tried
tell msg to make new attachment with properties {file name:"~/Library/img/image.png"} at after the last paragraph
but it can't find the image...
If I paste the path and search for it in my browser it comes up...
~/Library/img/image.png
Someone please tell me why this isn't working or if you know of a work around to get to the users folder name in AppleScript.

You can set a path like this:
set myPath to POSIX path of (path to library folder from user domain) & "img/image.png"

Related

Apple Mail attachments to Apple Notes via Applescript

I'm working on a script that copies Apple Mail mails into Apple Notes. So far so good, but I am having troubles getting the attachments into the newly created note. This is on macOS Catalina 10.15.7.
I spent half a day yesterday looking for solutions, but they all do not work.
When I execute a make new attachment statement, I get the following kind of error for each attachment found in the email: Notes got an error: Can’t make show id "x-coredata://32E6D9EF-97F3-424F-B151-DD166A00E0AB/ICNote/p9725" into type specifier. This UID is the same for each attachment.
I tried various path variations (as POSIX file, as text, or just as is) but all give the same error.
This simple code example already gives the error on line 2:
set theNote to make new note at folder NotesFolder with properties {body:theBody}
make new attachment at theNote with data theFileName
where theFileName is the complete path to the email-attachment, temporarily saved in a folder on the desktop so Applescript and Notes can have access to it.
Since the UID in the error message is the same for every found attachment, it can also be that theNote is not ok.
Any ideas what is wrong with this? Or is it Notes having problems (I've read that Notes in Catalina seems to have Applescripting bugs)
I'm at a loss, currently, so any help is much appreciated.
Can you create an attachment like so specifying your folder, account, and note id:
tell application "Notes"
activate
set theNote to make new note in folder "AB" of account "On My Mac" with properties {name:"3038665"}
tell theNote
make new attachment at end of attachments with data "/Users/xyz/Desktop/ab_download/test.jpg"
end tell
end tell

Apple Automator take screen capture and save to user-specified location

So, I'm trying to use Automator under Mac OS Yosemite to create a service to allow a user to take a screenshot and save it to a location they specify, through some sort of "Save As" dialog. It seemed like it should be easy, but for some reason I'm running into difficulty with it. The screenshot component is easy, using the "Take Screenshot" action in Automator, but it's the saving it to a custom location that's causing me problems.
After trying a few different approaches, it seemed the easiest thing to do was to save the screenshot to a fixed directory/filename from within the "Take Screenshot" action, and then (using AppleScript) rename it in that directory, and move it to the user-specified target directory. So, I added a "Run AppleScript" action to my service. In it, I generate the dialog to choose a file name/path, using the choose file name command in AppleScript. I'm trying to split up the file name from the path, so that I can rename the file I save in "Take Screenshot," and then move it to the path that I'd like to save it at. I can get the full path, but am having problems just getting the filename from the path—and I've tried a variety of suggestions from what I've seen online. In my screenshot, the error shown was from attempting to do
I'm not set by any means on this flow, so if anyone has any better suggestions on how to do what I'm trying to do, by all means please let me know. Otherwise, if someone's able to just tell me how I can extract the filename from the path (and also if there's some special way you have to use that string to rename the file) that'd be great!
AppleScript code pictured in screenshot:
on run {parameters}
set thePath to (choose file name with prompt "Where would you like to save your file?")
tell application "Finder"
display dialog thePath as string
end tell
set UnixPath to POSIX path of (thePath as text)
display dialog UnixPath
end run
I tried this but it didn't work:
set basePath to POSIX path of (parent of (thePath) as string)
Thanks for checking it out!
An easy way would be to use the command line tool "screencapture". It has many options you can choose. See its man page. Here's an example that you can run as an applescript directly or you could put this inside an applescript automator action if you want.
Good luck.
set thePath to (choose file name with prompt "Where would you like to save your file?")
do shell script "screencapture -mx -T1 " & quoted form of (POSIX path of thePath & ".png")
I've created a Folder action in Automator. Choose Desktop. Add Find Finder objects (search: Desktop). Add Move Finder Object and choose your preferred destination. This will automatically move all your screenshots.

Opening a Keynote Presentation using AppleScript (Keynote 6.5.2)

I am trying to see if i can change the text of some slides inside a keynote presentation that has already been created but i am having problems just opening it to begin with.
here is my current simple applescript:
set thisFile to "Users/myUserName/Desktop/KeynoteAppleScript.key"
tell application "Keynote"
activate
open thisFile
show slide 1
end tell
I get an error stating that the file I am trying to open :
"Users/myUserName/Desktop/KeynoteAppleScript.key" couldnt be opened
because there is no such file".
Thats obviously not correct, it is there and the i have double checked the name of the file to verify.
I am using Keynote 6.5.2
Script Editor Version 2.7, AppleScript 2.4
What am I doing wrong here?
Try adding a leading fwd slash to the path
e.g,
set thisFile to "/Users/myUserName/Desktop/KeynoteAppleScript.key"
I needed to do this to get it opening on 10.8, Keynote 5.3
ALSO..
When in doubt with a file path, try dragging the file from the desktop to the script window and it will insert a correct path.

Auto scripting two folders with the same name, but 1 folder appended with a prefix

I’m new to the forum and relatively new to scripting and wanted to ask a question.
I’m trying to script a workflow where when prompted, users create a folder and when the script executes, a dialogue pops up. Users name the folder, for example; NewFolder1, it is then saved in a directory. Another folder is then created in the background, into another directory with the same name created by the user earlier, but appended with a prefix, for example; Name_ NewFolder1.
I then want sub folders created in each directory as well as alias folders which point to Name_ NewFolder1 created in the original (NewFolder1) directory
I'm on a mac and I can do most of this via applescript or automator but am stuck on the initial creating 'NewFolder1' and 'Name_ NewFolder1' at the same time in the two separate directories.
I only want to prompt the user to name one folder rather than show them two dialogues for 'NewFolder1' and 'Name_ NewFolder1'
Does anyone have any ideas of how i can achieve this? Either via a shell, applescript or UNIX
Thank you
Here's how to do the part you're stuck on in AppleScript:
set folder_name to text returned of (display dialog "What is the folder name?" default answer "untitled")
set second_name to "Name_" & folder_name
tell application "Finder"
make new folder at desktop with properties {name:folder_name}
make new folder at desktop with properties {name:second_name}
end tell
`set client_name to text returned of (display dialog "Create Folders:" default answer "Type here")
set loc1 to ":path1"
set loc2 to ":path2"
set clientmedia_name to "name_" & client_name
tell application "Finder"
make new folder at loc2 with properties {name:clientmedia_name}
set new to make new folder at loc1 with properties {name:client_name}
make new folder at new with properties {name:"1"}
make new folder at new with properties {name:"2"}
make new folder at new with properties {name:"3"}
make new folder at new with properties {name:"4"}
end tell`
This is how far i have got, but can't seem to create folders in the clientmedia_name dir
Thanks,

Applescript to attach file to Mail Message. Worked in OSX 10.4 to 10.6 will not work in 10.8

With 10.8, My Applescript (developed when I was running 10.4 thru 10.6) that reads a comma separate text file, uses the values to create a new mail message, then attachs a xx.pdf file using a NSstring path name to the file, still sends out the email, but does not include the attachment. I understand from researching the problem, that 10.8 security enhancements have "SANDBOXed" the Mail app and applescript apps so that the path name no longer works as a valid, passible link to the file. I have found several examples that claim to fix the problem, Claiming that the NSstring "file name:" must be replaced with file name in NSURL format, but the code does not look like applescript, and will not compile in the applescript editor. Do you have an example of applescript that will attach the file to a new, applescript generated email message? I have been trying for days to solve this problem without success. I even called the Apple Support line, but they said the do not support Applescript related problems any more.
The variable that I pass in from the text file is named "theAttachment1". The line I am trying to get working is:
"make new attachment with properties {file name:theAttachment1} at after the last paragraph
Again, it works fine in 10.4 thru 10.6, but will not work in 10.8.
This works for me on 10.8
set theAttachment1 to POSIX file "/Users/USERNAME/Desktop/Desktop--IMAGE/scooter-6.jpg"
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:"subject", content:"the_content" & return & return}
tell newMessage
set visible to false
set sender to "myaddress#btinternet.com"
make new to recipient at end of to recipients with properties {address:"someAddress#btinternet.com"}
make new attachment with properties {file name:theAttachment1} at after the last paragraph
(* change save to send to send*)
save --<<<<---------------- change save to send to send
(* change save to send to send*)
end tell
end tell
The path can be either a Unix path :"/Users/USERNAME/Desktop/Desktop--IMAGE/scooter-6.jpg"
or a Posix file path : "Macintosh HD:Users:USERNAME:Desktop:Desktop--IMAGE:scooter-6.jpg"
In this script I converted the path from a Unix path to a Posix file path

Resources