• 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: 10.12.2 permission errors when saving
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 10.12.2 permission errors when saving


  • Subject: Re: 10.12.2 permission errors when saving
  • From: Yvan KOENIG <email@hidden>
  • Date: Wed, 04 Jan 2017 18:39:25 +0100

Here is a script allowing us to choose the mailbox whose attachments must be saved.
Some ASObjC instructions make part of the job.

--------------------------------------------------------------------------------
use AppleScript version "2.4" # Yosemite or later
use framework "Foundation"
use scripting additions

#=====#=====#=====#=====#=====#=====

on buildFullPath:proposedName inFolder:POSIXPath # appelé par une instruction
local |⌘|, theFolderURL, proposedName, theDestURL


set |⌘| to current application
set theFolderURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
if class of proposedName is text then set proposedName to |⌘|'s NSString's stringWithString:proposedName
# If proposedName is an Hfs name, it's converted into a POSIX one
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName


return theDestURL's |path| as text
end buildFullPath:inFolder:

#=====#=====#=====#=====#=====#=====

-- Creates a new folder. There is no error if the folder already exists, and it will also create intermediate folders if required
on createFolderNamed:proposedName inFolder:POSIXPath # appelé par une instruction
local |⌘|, theFolderURL, theDestURL, theFileManager


set |⌘| to current application
set theFolderURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
if class of proposedName is text then set proposedName to |⌘|'s NSString's stringWithString:proposedName
# If proposedName is an Hfs name, it's converted into a POSIX one
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
-- set {theResult, theError} to theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(missing value)
-- if not (theResult as boolean) then error (theError's |localizedDescription|() as text)


return theDestURL's |path| as text
end createFolderNamed:inFolder:

#=====#=====#=====#=====#=====#=====

on sortAList:theList
set theArray to current application's NSArray's arrayWithArray:theList
set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
return theArray as list
end sortAList:

#=====#=====#=====#=====#=====#=====

on replace:sourceString existingString:d1 newString:d2
set sourceString to current application's NSString's stringWithString:sourceString
return (sourceString's stringByReplacingOccurrencesOfString:d1 withString:d2) as text
end replace:existingString:newString:

#=====#=====#=====#=====#=====#=====

tell application "Mail"
set theProperties to properties of every mailbox


set boxNames to {}
# Concatenate if required the name of the container and the name of the box
# I'm not fond of this part
repeat with aBox in theProperties
tell aBox
set theName to its name
try
set theContainer to name of its container
set end of boxNames to theContainer & "/" & theName
on error
set end of boxNames to theName
end try
end tell # aBox
end repeat
end tell # Mail
# Sort the list of boxes
set boxNames to my sortAList:boxNames
# Select the one whose attachment(s) must be saved
using terms from scripting additions
set aBox to choose from list boxNames # returns a list or a boolean
end using terms from
# It's a boolean if we cancel the action
if class of aBox is boolean then error number -128
# Extracts the unique item of the list
set aBox to item 1 of aBox

set folderName to "attachments" # Change the name if you wish
set downloadFolder to (path to downloads folder as text) # Don't change this instruction
set targetFolder to POSIX path of (downloadFolder & "the attachments")
# This instruction create the two levels of subfolders if needed
set attachmentsFolder to my createFolderNamed:folderName inFolder:targetFolder

tell application "Mail"
set selectedMessages to every message of mailbox aBox


repeat with theMessage in selectedMessages
repeat with theAttachment in theMessage's mail attachments
set PosixName to name of theAttachment
# Builds the POSIX path to the saved file and convert it in a format accepted by Mail
set savePath to (my buildFullPath:PosixName inFolder:attachmentsFolder) as «class furl»
try
save theAttachment in savePath
end try
end repeat
end repeat


end tell

--------------------------------------------------------------------------------

I'm not sure that the scheme used to build the "full" name of every mailboxes is the best one and worse, it works only if we don't have more than two nested boxes.
I'm all ears if someone has a better code.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 4 janvier 2017 18:35:34





 _______________________________________________
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: 10.12.2 permission errors when saving
      • From: Yvan KOENIG <email@hidden>
References: 
 >Re: about your problem with attachments (From: Brian Christmas <email@hidden>)
 >Re: about your problem with attachments (From: Yvan KOENIG <email@hidden>)
 >Re: about your problem with attachments (From: Brian Christmas <email@hidden>)
 >Re: about your problem with attachments (From: Yvan KOENIG <email@hidden>)
 >Re: about your problem with attachments (From: Brian Christmas <email@hidden>)
 >Re: about your problem with attachments (From: Yvan KOENIG <email@hidden>)
 >10.12.2 permission errors when saving (From: Ray Robertson <email@hidden>)
 >Re: 10.12.2 permission errors when saving (From: Yvan KOENIG <email@hidden>)
 >Re: 10.12.2 permission errors when saving (From: Ray Robertson <email@hidden>)

  • Prev by Date: Re: 10.12.2 permission errors when saving
  • Next by Date: Re: Creating a list of numbers from a Contacts group
  • Previous by thread: Re: 10.12.2 permission errors when saving
  • Next by thread: Re: 10.12.2 permission errors when saving
  • Index(es):
    • Date
    • Thread