• 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: Code won't work in Tell block
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Code won't work in Tell block


  • Subject: Re: Code won't work in Tell block
  • From: Thomas Fischer <email@hidden>
  • Date: Mon, 01 Sep 2014 09:59:47 +0200

Hello Jennifer,

while Wayne's advice to use "my" should get your script running, you might consider to rewrite your clean_string subroutine, since this might take quite a long time if you're dealing with many entries.

I personally use the (free) Satimage.osax, which allows me to write something like

set cleanedName to my (change "[^a-z0-9]" into "" in quotedName with regexp without case sensitive)

If your AppleScript version does allow multiple text item delimiters (probably not on OS X 10.4), there is another solution if you're only interested in these kinds of replacements:
set AppleScript's text item delimiters to {" ", "(", ")", "-"}
set cleanedName to every text item of quotedName
set AppleScript's text item delimiters to {""}
set cleanedName to cleanedName as text

By the way, I don't think that you need to use "quoted form" in
set quotedName to quoted form of originalName
since anything strange will be erased by your cleaning actions (which would also eliminate any quoting of the string).
 
Best
Thomas


Am 01.09.2014 um 03:55 schrieb Jennifer:

Hello,

I'm having a problem with the following script and I could use some help.  clean_string() does not work inside the tell Mail block and the script stops there.  If I remove that portion of code and run it by itself, everything executes properly.  Can someone spot what's wrong?  I'm at a loss.  It's running under OS X 10.4.11.  

Everything was running fine until I added the clean_string() subroutine.  The reason I added that was just to strip out the characters "(", ")", "-", and spaces from the Subject.  I'm trying to turn this: "Greeting (404) 456-8897" into this: "greeting4044568897".  I know it's probably overkill so if there's a better way to do that, I'd appreciate hearing about it.  Thanks!

on perform_mail_action(ruleData)

-- The folder to save the attachments in (must already exist)
set attachmentsFolder to "Mac_HD:Library:Application Support:Phlink Items" as text
set AppleScript's text item delimiters to {""}

-- Get incoming messages that match the rule
tell application "Mail"
set selectedMessages to |SelectedMessages| of ruleData
repeat with theMessage in selectedMessages

-- Save the attachment
repeat with theAttachment in theMessage's mail attachments
set originalName to the subject of theMessage
set quotedName to quoted form of originalName
set cleanedName to clean_string(quotedName)
set lowerName to do shell script "echo " & cleanedName & " | tr A-Z a-z;"
set savePath to attachmentsFolder & ":" & lowerName & ".m4a"
tell application "Finder"
if file savePath exists then delete file savePath
end tell
delay 1
try
save theAttachment in savePath with replacing -- "with replacing" doesn't seem to work.  I had to have the Finder (above) delete the file (above) first before saving the new one.
end try
end repeat
end repeat
end tell

end tell

tell application "Ovolab Phlink" to activate

end perform_mail_action

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

on clean_string(TheString)
-- Remove all characters except alphanumeric
set potentialName to TheString
set legalName to {}
set legalCharacters to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
with timeout of 10 seconds

repeat with thisCharacter in the characters of potentialName
set thisCharacter to thisCharacter as text
if thisCharacter is in legalCharacters then
set the end of legalName to thisCharacter
if length of legalName is greater than 32 then
return legalName as text
end if
end if
end repeat
end timeout
return legalName as text
end clean_string

Thank you,
Jenni

Superior Shelving Systems::::....
The (Storage|Office|Home|Warehouse) Shelving Specialists
Since 1984

Wire LAN Shelving:
http://www.superiorshelving.com/mfg/nexel/pages/wire-shelving-chrome.php
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
  • Next by Date: Re: The Future of AppleScript
  • Next by thread: Re: The Future of AppleScript
  • Index(es):
    • Date
    • Thread