Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emails won't move to the selected folder #1

Open
AxelCorjon opened this issue Feb 21, 2023 · 8 comments
Open

Emails won't move to the selected folder #1

AxelCorjon opened this issue Feb 21, 2023 · 8 comments

Comments

@AxelCorjon
Copy link

Hi,

This is my first time using Alfred (and I haven't used apple script in a long time). But the script doesn't work for me :/

  • I configured a hotkey in Alfred
  • I can summon the interface and the email account folders are listed
  • When selecting a folder and hitting enter, nothing happens :/

I'm on macOS 13.1 and it's in French (a long time ago it was an issue with apple script, I don't know if it still is).

Could you help me?

@AxelCorjon AxelCorjon changed the title Doesn't work :/ Emails won't move to the selected folder Feb 21, 2023
@odapg
Copy link

odapg commented May 16, 2023

I believe that this is due to Mail having a different menu layout in Ventura.
Try to change "menu item 18" with "menu item 21" in the final "Run script" action.

@ldebritto
Copy link
Owner

I've changed the AppleScript of the second step on the workflow to a more straightforward move command. However, I'm still having trouble dealing with Gmail Accounts.

I guess I will keep an eye on how one could use both account and mailbox names in the menu approach, as it used to work even with Gmail accounts.

@Blog404DE
Copy link

I've changed the AppleScript of the second step on the workflow to a more straightforward move command. However, I'm still having trouble dealing with Gmail Accounts.

I guess I will keep an eye on how one could use both account and mailbox names in the menu approach, as it used to work even with Gmail accounts.

... and i even had trouble with every Account (one IMAP and one Exchange-Account). I've testet it under Ventura and Sonoma (current Version / Language set to: German) and it didn't move any Mail :-/

@odapg
Copy link

odapg commented Dec 29, 2023

@Blog404DE
I had the same problem. In "move command.applescript", I replaced the line
set mailbox of theMessage to theMailbox
with
move theMessage to theMailbox
(and put the line set theMailbox to mailbox mailboxName of account theAccount before of the repeat loop), and now it works fine.

@Blog404DE
Copy link

@odapg
hmm, i've changed the script to the following code:

on run argv
	tell application "Mail"
		try
			set mailboxName to item 1 of argv
			set theAccount to item 2 of argv
			if selection is {} then error
			set messageList to (selection as list)
			set theMailbox to mailbox mailboxName of account theAccount
			repeat with theMessage in messageList
				-- set theMessage to the first item of (selection as list)
				set theMailbox to mailbox mailboxName of account theAccount
				move theMessage to theMailbox
			end repeat
		end try
	end tell
end run

But it still didn't move any E-Mails and i have absolutely no idea how to debug a AppleScript inside of Alfred :-(

@odapg
Copy link

odapg commented Dec 30, 2023

@Blog404DE
Hmm. On my side it even works with my gmail account (on Sonoma 14.2.1).
Actually, my personal version is also modified at the level of the other applescript, in order to make it independent of JSON Helper.
So: I forked @ldebritto's project, and put my complete version on this fork. Perhaps can you give it a try? If it works, I could propose to @ldebritto to merge it (and this would probably mean that the problem comes from JSON Helper).

@Blog404DE
Copy link

thx :-) ... I've tried it with the same result. But i've found a option to debug my problem.

With activating the debugging mode in alfred, i've could created some log-Outputs. The first part of the script (creating a list auf mailboxes) is working without any problem (with your version) and i've testet the second script and it seems, the Problem started with the line:
set theMailbox to mailbox mailboxName of account theAccount
After this line, the script stopped and log "Debug-Point" didn't output anything inside Alfred's debugger. A debug output in the line directly in front of it, however, is still displayed and "mailboxName" / "theAccount" contains the correct information.

Maybe i've got time to dig deeper into the problem next year ... ;-)

@D4C4P0
Copy link

D4C4P0 commented Feb 3, 2025

I have absolutely no clue about coding, just what I got from high school and therefore a rudimentary knowledge. I googled for solutions a few days went back and forth. Found in apple communities and in macscripter discussions I used to tweak chatgpts o3-mini-high to follow this path, after several days running to nowhere. Anyways the code works now for me. I use an exchange on premise account and work in a German language environment. What I did: Making sure that folders are not subfolders of the Inbox. The code provided here, worked then. But not for sub folders. There seems to be a problem to get the right path for exchange accounts as you only get the mailbox name but when moving the mail to this mailbox it is expected that you provide the full path. Maybe someone with knowledge comes around to provide a better solution. Till then: I needed to make changes in the sorting script:

`use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


-- Handler: Retrieve the full mailbox path using an intentional error.
-- This handler attempts to get the container of a mailbox object.
-- For a top-level mailbox, this call raises an error that includes the full
-- mailbox path in quotes. We then extract the mailbox name from the error message.

on getMailboxPathUsingError(aMailbox)
try
-- Attempt to get the container of the mailbox.
-- For a top-level mailbox, this will raise an error.
set parentMailbox to container of aMailbox
-- If no error occurs, recursively call this handler and append the current mailbox name.
return (getMailboxPathUsingError(parentMailbox) & "/" & (name of aMailbox))
on error errMsg number errNum
-- Save the current text item delimiters.
set oldDelims to AppleScript's text item delimiters
-- Check if the error message contains the standard double quote (") delimiter.
if errMsg contains """ then
set AppleScript's text item delimiters to """
else if errMsg contains "“" then
set AppleScript's text item delimiters to "“"
else
return (name of aMailbox)
end if
-- Split the error message into text items.
set parts to text items of errMsg
-- Restore the original text item delimiters.
set AppleScript's text item delimiters to oldDelims
-- If the error message is formatted as expected, the mailbox name should be the second item.
if (count of parts) ≥ 3 then
return item 2 of parts
else
return (name of aMailbox)
end if
end try
end getMailboxPathUsingError


-- Main script: Build a list of all mailboxes in the current account with their
-- full paths, so that the list can be filtered in Alfred.

set mailboxList to {}

tell application "Mail"
-- Retrieve the current selection as a list.
try
if selection is {} then error "No message selected"
set theMessage to the first item of (selection as list)
set currentMailbox to mailbox of theMessage
set theAccount to the account of currentMailbox
on error errMsg
display dialog "Error: " & errMsg
return
end try

-- Get all top-level mailboxes for the account.
set allAccountMailboxes to every mailbox of theAccount

-- For each mailbox, determine the full path and create a record for Alfred.
repeat with eachMailbox in allAccountMailboxes
	-- Call the handler with "my" so that it is executed in the script context.
	set fullPath to my getMailboxPathUsingError(eachMailbox)
	set thisRecord to {uid:(fullPath & "-" & (name of theAccount)), ¬
		title:fullPath, ¬
		subtitle:(name of theAccount), ¬
		arg:{fullPath, (name of theAccount)}, ¬
		valid:true, match:fullPath}
	set end of mailboxList to thisRecord
end repeat

-- Package the list into a dictionary for the JSON Helper.
set |items| to {|items|:mailboxList}

end tell

tell application "JSON Helper"
-- Generate JSON output with pretty printing.
set mailboxList to make JSON from ¬
|items| with pretty printing
end tell

return mailboxList`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants