From c966acff06b04cc53840d475da910ed88d025ff6 Mon Sep 17 00:00:00 2001 From: Xeric Date: Sat, 29 Feb 2020 12:11:45 +0800 Subject: [PATCH] fix recent function, support better way to detect default profile folder --- info.plist | 436 +++++++++++++++++++++++++++++++++++------------------ main.py | 10 +- util.py | 63 +++++--- 3 files changed, 338 insertions(+), 171 deletions(-) diff --git a/info.plist b/info.plist index a6233f3..cb847c5 100644 --- a/info.plist +++ b/info.plist @@ -10,6 +10,16 @@ 0056A321-AB40-470C-8994-988B75DAA917 + + destinationuid + 209D79AC-0B39-4970-98F0-0B0D714DB3DC + modifiers + 0 + modifiersubtext + + vitoclose + + destinationuid 744E406F-659B-4E95-9A20-6B39A152E3A8 @@ -144,6 +154,19 @@ + 209D79AC-0B39-4970-98F0-0B0D714DB3DC + + + destinationuid + B9D849E1-328E-4D60-B65D-CA478174E27B + modifiers + 0 + modifiersubtext + + vitoclose + + + 395D0273-E993-49F3-A97E-66AD47BC717C @@ -216,11 +239,21 @@ - 739D3F5E-20FE-48BF-9A51-07CEBAE8C00D + 62E99127-9A2B-4C67-805C-566A80C3C9D4 destinationuid - 56408975-1DD7-44FD-BD95-C1C34FAC2F7C + 744E406F-659B-4E95-9A20-6B39A152E3A8 + modifiers + 0 + modifiersubtext + + vitoclose + + + + destinationuid + CDCAAABA-759D-4D85-8399-07197A7A4C71 modifiers 0 modifiersubtext @@ -229,11 +262,11 @@ - 744E406F-659B-4E95-9A20-6B39A152E3A8 + 739D3F5E-20FE-48BF-9A51-07CEBAE8C00D destinationuid - 8D20D177-4409-4C23-9DDA-25DA725B5804 + 56408975-1DD7-44FD-BD95-C1C34FAC2F7C modifiers 0 modifiersubtext @@ -241,9 +274,12 @@ vitoclose + + 744E406F-659B-4E95-9A20-6B39A152E3A8 + destinationuid - B9D849E1-328E-4D60-B65D-CA478174E27B + 8D20D177-4409-4C23-9DDA-25DA725B5804 modifiers 0 modifiersubtext @@ -438,46 +474,6 @@ Outlook Search objects - - config - - lastpathcomponent - - onlyshowifquerypopulated - - removeextension - - text - Files saved to {query} - title - Success! - - type - alfred.workflow.output.notification - uid - 8D20D177-4409-4C23-9DDA-25DA725B5804 - version - 1 - - - config - - inputstring - {query} - matchcasesensitive - - matchmode - 2 - matchstring - ^(?!olkwarning.*$).* - - type - alfred.workflow.utility.filter - uid - 744E406F-659B-4E95-9A20-6B39A152E3A8 - version - 1 - config @@ -489,14 +485,66 @@ on run argv set q to item 1 of argv tell application "Microsoft Outlook" - set configuredSavePath to system attribute "olk_attachment_path" set savePath to POSIX path of (do shell script ("echo " & configuredSavePath)) set myAttachmentsFolder to savePath - set msgID to my replace_chars(q, "download_", "") set msg to get message id (msgID as integer) set all_attachments to attachments of msg + set configuredSavePath to system attribute "olk_attachment_path" + set savePath to POSIX path of (do shell script ("echo " & configuredSavePath)) + set myAttachmentsFolder to savePath + set msgID to my replace_chars(q, "download_", "") + set msg to get message id (msgID as integer) + set all_attachments to attachments of msg if all_attachments is {} return "olkwarning: No attachment found!" - end if set subj to subject of msg set msgSubject to subj as string set newSubject to my cleanSubject(msgSubject) set saveToFolder to myAttachmentsFolder & "/" & msgID & "- " & newSubject tell application "Finder" if not (exists saveToFolder) then do shell script "mkdir -p " & quoted form of saveToFolder end if end tell repeat with this_attachment in all_attachments try set attName to (get the name of this_attachment) set saveAsName to saveToFolder & "/" & attName save this_attachment in POSIX file saveAsName on error exit repeat end try end repeat - return saveToFolder end tell + end if + set subj to subject of msg + set msgSubject to subj as string + set newSubject to my cleanSubject(msgSubject) + set saveToFolder to myAttachmentsFolder & "/" & msgID & "- " & newSubject + tell application "Finder" + if not (exists saveToFolder) then + do shell script "mkdir -p " & quoted form of saveToFolder + end if + end tell + repeat with this_attachment in all_attachments + try + set attName to (get the name of this_attachment) + set saveAsName to saveToFolder & "/" & attName + save this_attachment in POSIX file saveAsName + on error + exit repeat + end try + end repeat + return "File Save to " & saveToFolder + end tell end run -on cleanSubject(this_text) set c to my replace_chars(this_text, ":", "_") set c1 to my replace_chars(c, "'", "_") set c2 to my replace_chars(c1, "\"", "_") set c3 to my replace_chars(c2, "!", "_") set c4 to my replace_chars(c3, "@", "_") set c5 to my replace_chars(c4, "#", "_") set c6 to my replace_chars(c5, "$", "_") set c7 to my replace_chars(c6, "%", "_") set c8 to my replace_chars(c7, "^", "_") set c9 to my replace_chars(c8, "&", "_") set c10 to my replace_chars(c9, "*", "_") set c11 to my replace_chars(c10, "(", "_") set c12 to my replace_chars(c11, ")", "_") set c13 to my replace_chars(c12, "-", "_") set c14 to my replace_chars(c13, "/", "_") set c15 to my replace_chars(c14, ":", "_") return c15 end cleanSubject on replace_chars(theText, theSearchString, theReplacementString) set AppleScript's text item delimiters to theSearchString set theTextItems to every text item of theText set AppleScript's text item delimiters to theReplacementString set theText to theTextItems as string set AppleScript's text item delimiters to "" return theText end replace_chars on pad(n) return text -2 thru -1 of ("00" & n) end pad +on cleanSubject(this_text) + set c to my replace_chars(this_text, ":", "_") + set c1 to my replace_chars(c, "'", "_") + set c2 to my replace_chars(c1, "\"", "_") + set c3 to my replace_chars(c2, "!", "_") + set c4 to my replace_chars(c3, "@", "_") + set c5 to my replace_chars(c4, "#", "_") + set c6 to my replace_chars(c5, "$", "_") + set c7 to my replace_chars(c6, "%", "_") + set c8 to my replace_chars(c7, "^", "_") + set c9 to my replace_chars(c8, "&", "_") + set c10 to my replace_chars(c9, "*", "_") + set c11 to my replace_chars(c10, "(", "_") + set c12 to my replace_chars(c11, ")", "_") + set c13 to my replace_chars(c12, "-", "_") + set c14 to my replace_chars(c13, "/", "_") + set c15 to my replace_chars(c14, ":", "_") + return c15 +end cleanSubject +on replace_chars(theText, theSearchString, theReplacementString) + set AppleScript's text item delimiters to theSearchString + set theTextItems to every text item of theText + set AppleScript's text item delimiters to theReplacementString + set theText to theTextItems as string + set AppleScript's text item delimiters to "" + return theText +end replace_chars +on pad(n) + return text -2 thru -1 of ("00" & n) +end pad scriptargtype 1 scriptfile @@ -511,23 +559,6 @@ on cleanSubject(this_text) set c to my replace_chars(this_text, ":", "_") set version 2 - - config - - autopaste - - clipboardtext - {query} - transient - - - type - alfred.workflow.output.clipboard - uid - B9D849E1-328E-4D60-B65D-CA478174E27B - version - 2 - config @@ -547,25 +578,6 @@ on cleanSubject(this_text) set c to my replace_chars(this_text, ":", "_") set version 1 - - config - - inputstring - {query} - matchcasesensitive - - matchmode - 2 - matchstring - ^olkwarning:.+ - - type - alfred.workflow.utility.filter - uid - CDCAAABA-759D-4D85-8399-07197A7A4C71 - version - 1 - config @@ -600,44 +612,6 @@ on cleanSubject(this_text) set c to my replace_chars(this_text, ":", "_") set version 1 - - config - - matchmode - 1 - matchstring - ^olkwarning: - replacestring - - - type - alfred.workflow.utility.replace - uid - 4009E6EB-CAC0-44D7-8EE2-0348427E53DE - version - 1 - - - config - - lastpathcomponent - - onlyshowifquerypopulated - - removeextension - - text - {query} - title - Warning! - - type - alfred.workflow.output.notification - uid - EF0EA5A2-CF83-49F3-B150-CD630E057FA0 - version - 1 - config @@ -701,6 +675,23 @@ python main.py "$query" version 2 + + config + + autopaste + + clipboardtext + {query} + transient + + + type + alfred.workflow.output.clipboard + uid + B9D849E1-328E-4D60-B65D-CA478174E27B + version + 2 + config @@ -758,6 +749,25 @@ python main.py "$query" version 1 + + config + + inputstring + {query} + matchcasesensitive + + matchmode + 2 + matchstring + ^(?!olkwarning.*$).* + + type + alfred.workflow.utility.filter + uid + 209D79AC-0B39-4970-98F0-0B0D714DB3DC + version + 1 + config @@ -777,6 +787,27 @@ python main.py "$query" version 1 + + config + + lastpathcomponent + + onlyshowifquerypopulated + + removeextension + + text + {query} + title + Success! + + type + alfred.workflow.output.notification + uid + 8D20D177-4409-4C23-9DDA-25DA725B5804 + version + 1 + config @@ -864,6 +895,25 @@ python conf.py "$query" version 2 + + config + + inputstring + {query} + matchcasesensitive + + matchmode + 2 + matchstring + ^(?!olkwarning.*$).* + + type + alfred.workflow.utility.filter + uid + 744E406F-659B-4E95-9A20-6B39A152E3A8 + version + 1 + config @@ -902,6 +952,27 @@ python conf.py "$query" version 1 + + config + + lastpathcomponent + + onlyshowifquerypopulated + + removeextension + + text + {query} + title + Warning! + + type + alfred.workflow.output.notification + uid + EF0EA5A2-CF83-49F3-B150-CD630E057FA0 + version + 1 + config @@ -921,6 +992,42 @@ python conf.py "$query" version 1 + + config + + inputstring + {query} + matchcasesensitive + + matchmode + 2 + matchstring + ^olkwarning:.+ + + type + alfred.workflow.utility.filter + uid + CDCAAABA-759D-4D85-8399-07197A7A4C71 + version + 1 + + + config + + matchmode + 1 + matchstring + ^olkwarning: + replacestring + + + type + alfred.workflow.utility.replace + uid + 4009E6EB-CAC0-44D7-8EE2-0348427E53DE + version + 1 + config @@ -1009,7 +1116,18 @@ python contact.py "$query" config applescript - # thanks for a scripts inspire me in a workflow with no author in internet on alfred_script(q) tell application "Microsoft Outlook" activate set newMessage to make new outgoing message if q is not "" then make new recipient at newMessage with properties {email address:{address:q}} end if open newMessage end tell end alfred_script + # thanks for a scripts inspire me in a workflow with no author in internet + +on alfred_script(q) + tell application "Microsoft Outlook" + activate + set newMessage to make new outgoing message + if q is not "" then + make new recipient at newMessage with properties {email address:{address:q}} + end if + open newMessage + end tell +end alfred_script cachescript @@ -1180,16 +1298,39 @@ python newmail.py "$query" config applescript - # thanks for a scripts inspire me in a workflow with no author in internet on alfred_script(q) + # thanks for a scripts inspire me in a workflow with no author in internet + +on alfred_script(q) set [receiver, msg] to get_receiver_and_content(q) - tell application "Microsoft Outlook" -- activate - set myAccount to exchange account 1 tell me to log domain of myAccount as text tell me to log is connected of myAccount as text set newMessage to make new outgoing message with properties {subject: msg, content: msg, account:myAccount} make new recipient at newMessage with properties {email address:{name: receiver, address: receiver}} send newMessage tell me to log was sent of newMessage as text - -- open newMessage end tell end alfred_script + + tell application "Microsoft Outlook" + -- activate + try + set myAccount to default account + tell me to log domain of myAccount as text + tell me to log is connected of myAccount as text + set newMessage to make new outgoing message with properties {subject: msg, content: msg, account:myAccount} + + make new recipient at newMessage with properties {email address:{name: receiver, address: receiver}} + + send newMessage + tell me to log was sent of newMessage as text + -- open newMessage + on error + return "olkwarning: Send Inline Mail Failed" + end try + + return "Send Inline Mail to " & receiver & " Complete" + end tell +end alfred_script on get_receiver_and_content(str) - set the_first_space_location to offset of " " in str set split_before to the_first_space_location - 1 set split_after to the_first_space_location + 1 + set the_first_space_location to offset of " " in str + set split_before to the_first_space_location - 1 + set split_after to the_first_space_location + 1 set last_p to the length of str - set receiver to text 1 thru split_before of str as string set msg to text split_after thru last_p of str as string + set receiver to text 1 thru split_before of str as string + set msg to text split_after thru last_p of str as string return [receiver, msg] end get_receiver cachescript @@ -1314,6 +1455,13 @@ https://github.com/xeric/alfred-outlook ypos 310 + 209D79AC-0B39-4970-98F0-0B0D714DB3DC + + xpos + 1050 + ypos + 400 + 2DB07F3A-9BF1-4FCD-B19F-0EE1232C2154 xpos @@ -1331,9 +1479,9 @@ https://github.com/xeric/alfred-outlook 4009E6EB-CAC0-44D7-8EE2-0348427E53DE xpos - 960 + 1130 ypos - 310 + 670 407DC3A9-37B0-43BD-85A4-1CC8740BF2D1 @@ -1373,9 +1521,9 @@ https://github.com/xeric/alfred-outlook 744E406F-659B-4E95-9A20-6B39A152E3A8 xpos - 880 + 1050 ypos - 110 + 530 7542A242-1841-460B-913A-541F32EF0196 @@ -1394,9 +1542,9 @@ https://github.com/xeric/alfred-outlook 8D20D177-4409-4C23-9DDA-25DA725B5804 xpos - 1040 + 1210 ypos - 70 + 500 8DB394A4-5F25-4E2C-AED5-C9EBED7FB718 @@ -1450,9 +1598,9 @@ https://github.com/xeric/alfred-outlook B9D849E1-328E-4D60-B65D-CA478174E27B xpos - 1040 + 1210 ypos - 190 + 370 BDA1DBAF-FF52-4C96-9310-C6A7BB563E41 @@ -1478,9 +1626,9 @@ https://github.com/xeric/alfred-outlook CDCAAABA-759D-4D85-8399-07197A7A4C71 xpos - 880 + 1050 ypos - 230 + 670 CE568D89-8C12-4D56-A843-DD1A0421C68B @@ -1499,9 +1647,9 @@ https://github.com/xeric/alfred-outlook EF0EA5A2-CF83-49F3-B150-CD630E057FA0 xpos - 1040 + 1210 ypos - 310 + 640 variables @@ -1512,7 +1660,7 @@ https://github.com/xeric/alfred-outlook ~/Downloads/outlook_attachments/ version - 0.1.8 + 0.1.9 webaddress https://github.com/xeric/alfred-outlook diff --git a/main.py b/main.py index 9e4913d..1bfe9ba 100644 --- a/main.py +++ b/main.py @@ -185,13 +185,19 @@ def handle(wf, query): if page > 1: previousPage = 0 if page - 2 < 0 else page - 2 queryByVersion = originalQuery if not Util.isAlfredV2(wf) else originalQuery + '|' + str(previousPage) + subtitle = 'click to retrieve previous ' + str(calculatedPageSize) + ' results' it = wf.add_item(title='No More Results', - subtitle='click to retrieve previous ' + str(calculatedPageSize) + ' results', + subtitle=subtitle, arg=queryByVersion, uid='z' + str(random.random()), valid=True) if not Util.isAlfredV2(wf): it.setvar('page', previousPage) + mod = it.add_modifier(key='ctrl', + subtitle=subtitle, + arg=queryByVersion, + valid=True) + mod.setvar('page', previousPage) cur.close() wf.send_feedback() @@ -286,7 +292,7 @@ def queryAll(cur, keywords, offset, pageSize, folder, top = -1): titleVars = [] senderVars = [] contentVars = [] - conditions = None + conditions = " 1 = 1 " # a trick for reduce add additional "AND" in Query for kw in keywords: titleVars.append('%' + kw + '%') diff --git a/util.py b/util.py index 77575d8..04fc508 100644 --- a/util.py +++ b/util.py @@ -3,6 +3,17 @@ import os from consts import * +import workflow + +APPLE_SCRIPT_DEFAULT_PROFILE = """ +tell application "Microsoft Outlook" + set theAlias to current identity folder as alias + tell application "Finder" + return name of theAlias + end tell +end tell +""" + class Util(object): @staticmethod @@ -11,31 +22,33 @@ def isAlfredV2(wf): @staticmethod def configureDefaultProfile(wf): - homePath = os.environ['HOME'] - - defaultProfilePath = homePath + OUTLOOK_DATA_PARENT + OUTLOOK_DEFAULT_PROFILE - confFolder = OUTLOOK_DEFAULT_PROFILE - - if not os.path.isdir(defaultProfilePath): - # list all folders - parent = homePath + OUTLOOK_DATA_PARENT - profileFolders = os.listdir(parent) - - largestProfileSize = 0 - largestProfile = None - for profile in profileFolders: - if not profile.startswith('.'): - if Util.validateProfile(parent + profile + OUTLOOK_DATA_FOLDER): - size = os.path.getsize(parent + profile + OUTLOOK_DATA_FOLDER + OUTLOOK_SQLITE_FILE) - if size > largestProfileSize: - largestProfile = profile - - confFolder = largestProfile - - if confFolder: - wf.logger.info("finally, we set profile to " + str(confFolder) + " as default profile conf!") - else: - wf.logger.info("no profile found") + confFolder = workflow.util.run_applescript(APPLE_SCRIPT_DEFAULT_PROFILE) + confFolder = confFolder.strip() + # homePath = os.environ['HOME'] + # + # defaultProfilePath = homePath + OUTLOOK_DATA_PARENT + OUTLOOK_DEFAULT_PROFILE + # confFolder = OUTLOOK_DEFAULT_PROFILE + # + # if not os.path.isdir(defaultProfilePath): + # # list all folders + # parent = homePath + OUTLOOK_DATA_PARENT + # profileFolders = os.listdir(parent) + # + # largestProfileSize = 0 + # largestProfile = None + # for profile in profileFolders: + # if not profile.startswith('.'): + # if Util.validateProfile(parent + profile + OUTLOOK_DATA_FOLDER): + # size = os.path.getsize(parent + profile + OUTLOOK_DATA_FOLDER + OUTLOOK_SQLITE_FILE) + # if size > largestProfileSize: + # largestProfile = profile + # + # confFolder = largestProfile + # + # if confFolder: + # wf.logger.info("finally, we set profile to " + str(confFolder) + " as default profile conf!") + # else: + # wf.logger.info("no profile found") wf.store_data(KEY_PROFILE, confFolder) @staticmethod