2014-01-30 112 views
0

我期待構建一個將桌面Gmail郵件鏈接作爲輸入的Applescript,並輸出一個可在iOS上工作的URL,以便在iOS中打開相同的郵件Gmail應用。Applescript將桌面Gmail鏈接轉換爲iOS Gmail應用鏈接

下面是一個典型的Gmail網址:

https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f

而且這裏是相同的URL將需要看起來像在iOS工作:

googlegmail:/// CV = 143c5ddc34313e1f /帳戶ID = 2

一對夫婦說明:

最重要的部分(線程標識符)是在鄰串的最後部分嚴謹的桌面網址。這就是移動URL中cv =之後需要去做的事情。但是,由於Gmail允許多帳戶登錄,因此我們還需要在桌面上爲/ 1的移動設備上記下賬戶ID號碼,而移動設備上的移動設備號碼爲2,這看起來像是桌面URL從0開始編號,而移動網址則從1開始編號,具體取決於您首先登錄哪個帳戶。所以我們需要爲iOS網址增加帳號ID號碼1。

此外,我不確定在「#inbox」之前「?zx = tso6hataagpp」是什麼。我發現有時我的桌面Gmail網址包含該部分,而其他時間則不包含(但仍包含#inbox)。我認爲這不重要,因爲我們想要的重要部分位於字符串的末尾,以及始終一致的「mail.google.com/mail/u/」後面的數字。

理想情況下,AppleScript的會看着剪貼板桌面Gmail的URL,如果發現一個,它會輸出相同的URL,然後換行,那麼iOS網址緊隨其後,像這樣:

https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f googlegmail:/// CV = 143c5ddc34313e1f /帳戶ID = 2

任何AppleScript的大師在那裏,可以告訴我怎麼破解起來呢?

回答

0

我能想到的最簡單的方法是:

這被固定的路徑組件。即組件5和最後一個項目

--https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f 

set pathUrl to (the clipboard) 
set pathComponents to words of pathUrl 
if item 1 of pathComponents is "https" and item 2 of pathComponents is "mail.google.com" then 

    set composed to pathUrl & return & "googlegmail:///cv=" & last item of pathComponents & "/accountId=" & (((item 5 of pathComponents) as number) + 1) 
end if 

這可以確保後都能得到組分(賬戶)的「U」,無論它在哪裏。 你可以爲消息ID做同樣的事情。通過使用「收件箱」

set composed to "" 
    --set pathUrl to "https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f" 
    set pathUrl to (the clipboard) 
    set pathComponents to words of pathUrl 
    if item 1 of pathComponents is "https" and item 2 of pathComponents is "mail.google.com" then 

     repeat with i from 1 to number of items in pathComponents 
      set this_item to item i of pathComponents 
      if this_item is "u" then 
       set this_item to item (i + 1) of pathComponents 
       set composed to pathUrl & return & "googlegmail:///cv=" & last item of pathComponents & "/accountId=" & ((this_item as number) + 1) 
exit repeat 
      end if 
     end repeat 

    end if 

    composed 
0

請嘗試以下操作 - 通過do shell script將解析委託給bash。

# Get text from clipboard. 
# Test with: "https://mail.google.com/mail/u/1/?zx=tso6hataagpp#inbox/143c5ddc34313e1f" 
set hLink to (the clipboard as text) 

# Parse the link to extract the information of interest. 
set parseResult to do shell script ¬ 
    "[[ " & quoted form of hLink & " =~ /u/([0-9]+)/.+#inbox/(.+)$ ]] && 
    printf '%s\\n' \"${BASH_REMATCH[1]}\" \"${BASH_REMATCH[2]}\"" 

# Synthesize the Gmail for iOS link. 
set gmLink to "googlemail:///cv=" & ¬ 
    (paragraph 2 of parseResult) & "/accountId=" & (1 + (paragraph 1 of parseResult)) 

# Synthesize the result 
set res to hLink & linefeed & gmLink 

# Put the result back on the clipboard. 
set the clipboard to res