Re: Save/export attachment in Apple Notes
Re: Save/export attachment in Apple Notes
- Subject: Re: Save/export attachment in Apple Notes
- From: Mike Li via AppleScript-Users <email@hidden>
- Date: Tue, 07 Jan 2025 20:38:21 -0800
- Feedback-id: i17794915:Fastmail
Hi Joel,
Thanks for the quick help. It throws this error on line : firstAttachment.file()
Error: Can't get object.
Seems the attachment object doesn't have file() method on it?
On Tue, Jan 7, 2025, at 6:54 PM, Joel Esler wrote:
> Try this:
>
> function run(argv) {
> const folderName = "YOUR_FOLDER_NAME_HERE"; // Replace with your folder name
> const savePath = "/Users/yourusername/Desktop/"; // Adjust as needed
>
> const Notes = Application("Notes");
> const app = Application.currentApplication();
> app.includeStandardAdditions = true;
>
> let result = {
> noteName: null,
> attachmentSaved: false,
> savedPath: null,
> error: null,
> };
>
> try {
> // Locate the folder
> const blogFolder = Notes.folders.whose({ name: folderName })[0];
> if (!blogFolder) throw new Error(`Folder "${folderName}" not found.`);
>
> // Get the first note in the folder
> const firstNote = blogFolder.notes()[0];
> if (!firstNote) throw new Error("No notes found in the folder.");
>
> result.noteName = firstNote.name();
>
> // Check for attachments
> const attachments = firstNote.attachments();
> if (attachments.length === 0) throw new Error("No attachments found in
> the note.");
>
> // Process the first attachment
> const firstAttachment = attachments[0];
> const attachmentName = firstAttachment.name();
> const fullSavePath = savePath + attachmentName;
>
> // Save the attachment as raw data
> const attachmentData = firstAttachment.file();
> if (!attachmentData) throw new Error("Unable to retrieve attachment
> data.");
>
> app.doShellScript(`echo '${attachmentData.toString()}' >
> '${fullSavePath}'`);
> result.attachmentSaved = true;
> result.savedPath = fullSavePath;
> } catch (error) {
> result.error = error.toString();
> }
>
> return JSON.stringify(result, null, 2);
> }
>
> —
> j
>
> > On Jan 7, 2025, at 20:27, Mike Li via AppleScript-Users
> > <email@hidden> wrote:
> >
> >
> > Hi AppleScript users,
> >
> > I am trying to write some AppleScript (or JXA) to get the attachment in
> > Notes to a folder. However, I am constantly encounter permission issues or
> > expected type errros.
> >
> > function run(argv) {
> > const folderName = "YOUR_FOLDER_NAME_HERE";
> > const savePath = "/Users/yourusername/Desktop/"; // Adjust this path as
> > needed
> >
> > const Notes = Application("Notes");
> > const app = Application.currentApplication();
> > app.includeStandardAdditions = true;
> >
> > const blogFolder = Notes.folders.whose({ name: folderName })[0];
> > const firstNote = blogFolder.notes()[0];
> >
> > let result = {
> > noteName: firstNote.name(),
> > attachmentSaved: false,
> > savedPath: null,
> > error: null
> > };
> >
> > const attachments = firstNote.attachments();
> >
> > if (attachments.length > 0) {
> > const firstAttachment = attachments[0];
> > const fullSavePath = savePath + firstAttachment.name();
> >
> > try {
> > firstAttachment.save(fullSavePath);
> > result.attachmentSaved = true;
> > result.savedPath = fullSavePath;
> > } catch (error) {
> > result.error = `Failed to save attachment: ${error}`;
> > }
> > } else {
> > result.error = "No attachments found in the first note.";
> > }
> >
> > return JSON.stringify(result, null, 2);
> > }
> >
> >
> > I also tried using const output = Path(...) and then run
> > firstAttachment.save({to: fullSavePath}); but still got type error.
> >
> > Appreciate any help on how to export attachments in Apple Notes either
> > through AppleScript or JXA!
> >
> > Thank!
> >
> > Mike
> > _______________________________________________
> > 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
>
_______________________________________________
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