14 Replies
      Latest reply on Apr 26, 2020 9:20 PM by sstanley
      rbononno Level 1 Level 1 (0 points)

        Maybe someone here can assist me with this.

         

        I have an older AppleScript called zip-to-mail, which I modified to work with my gmail account, etc. It's a relatively simple script that locates a file in the Finder, zips the file, creates a new email, adds the file name and current date as the subject line, attaches the zipped file, mails it to my gmail account, and deletes the zipped file. I have used it for years with several versions of MacOS and it's very convenient. At some point in 2017, unbeknownst to me, the script stopped working. It appeared to be working but the attachments were missing. I happened to look for an archived file from my gmail account the other day and there was no attachment. There was no attachment for the previous message either or the one before that or before that, all the way back to mid-2017. I never saw an error message and the script always ran. If I recall correctly, it was at that point in 2017 that I upgrade my iMac from Mavericks to Sierra. I am now running Mojave, which throws up a couple of security messages during execution of the script (to which I can click OK). However, my point is that the script is not attaching the zip file as it should and I don't know why. Anyone? The original script, called zip-to-mail, was in fairly common use for several years I believe; simply modified the existing script to suit my personal situation. I can post the code here of course. Recompiling the script didn't help.

        • Re: AppleScript zip-to-mail stops working
          rbononno Level 1 Level 1 (0 points)

          When I run this script, there appear to be a couple of errors in the log, maybe someone can help me resolve them. I'd be happy to post the entire code (it's a short script), but they are as follows:

           

          1.

            current date

            --> error number -10004

           

          2.

            make new attachment with properties {file name:"/Users/robertb/Desktop/some_file.zip"} at before last paragraph of content of outgoing message id 4

            --> missing value

           

          Thanks.

            • Re: AppleScript zip-to-mail stops working
              eskimo Apple Staff Apple Staff (13,905 points)

              Well, let’s start with the first error.  Does current date only fail in the context of your script?  Or if you do the following does it also fail?

              1. Run Script Editor.

              2. Create a new script (File > New).

              3. Enter current date as the script.

              4. Run it.

              Share and Enjoy

              Quinn “The Eskimo!”
              Apple Developer Relations, Developer Technical Support, Core OS/Hardware
              let myEmail = "eskimo" + "1" + "@apple.com"

                • Re: AppleScript zip-to-mail stops working
                  rbononno Level 1 Level 1 (0 points)

                  That works fine with the following result:

                   

                  date "Tuesday, April 14, 2020 at 8:01:32 PM"

                    • Re: AppleScript zip-to-mail stops working
                      eskimo Apple Staff Apple Staff (13,905 points)

                      It’s curious that it’s then failing in your main script.  In your main script, are you trying to run current date in the context of the target app, for example, in a tell block that targets Mail?

                      tell application "Mail"
                          current date
                      end tell

                      Share and Enjoy

                      Quinn “The Eskimo!”
                      Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                      let myEmail = "eskimo" + "1" + "@apple.com"

                        • Re: AppleScript zip-to-mail stops working
                          rbononno Level 1 Level 1 (0 points)

                          The problem isn't the date. The date is correctly inserted into the outgoing mail. The problem is that the attachment no longer gets attached to the message. The script is short so I'll post the whole thing. The problem seems to be happening near the bottom. I've made a couple of change to anonymize the script (the parts in italic: my_name, etc.).

                           

                          ----

                          set aFile to (choose file)

                          set zipFilePath to fileZipper(aFile)

                           

                          --Handler to make .zip files (from file or folder path)

                          on fileZipper(myFile)

                            -- deal with folders

                            if (myFile as text) ends with ":" then

                            set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}

                            set myFile to (text items 1 thru -2 of (myFile as text) as text)

                            set AppleScript's text item delimiters to TIDs

                            end if

                            set zipFile to (myFile & ".zip") as text

                            try

                            do shell script "/usr/bin/ditto -c -k -rsrc --keepParent " & (quoted form of (POSIX path of myFile)) & " " & (quoted form of (POSIX path of zipFile))

                            on error theError

                            display dialog theError

                            end try

                           

                           

                            --

                            tell application "Finder"

                            set shortname to name of file (zipFile)

                            set theSubject to shortname & " enclosed -- " & (current date) as text

                            set theBody to "Zip archive enclosed."

                            set theName to "my_name"

                            set theAddress to "my-email@gmail.com"

                            set theSender to "firstname_lastname <me@myisp.com>"

                            set theAttachment to POSIX path of zipFile

                           

                           

                            tell application "Mail"

                            set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}

                            tell newMessage

                           

                            -- Default is false. Determines whether the compose window will

                            -- show on the screen or whether it will happen in the background.

                           

                            set visible to true

                            set sender to theSender

                            make new to recipient at end of to recipients with properties {name:theName, address:theAddress}

                           

                            tell content

                            make new attachment with properties {file name:theAttachment} at before the last paragraph

                            end tell

                            end tell

                           

                            -- Bring the new compose window to the foreground, in all its glory

                            activate

                            send newMessage

                            end tell

                            end tell

                            --the following line deletes the recently created zipFile

                            do shell script "rm " & (quoted form of (POSIX path of zipFile))

                          end fileZipper

                          --end

                          --

                           

                          If I run this and look at the log, I find the following:

                           

                          make new attachment with properties {file name:"/Users/robertb/Desktop/test-file.pdf.zip"} at before last paragraph of content of outgoing message id 4

                            --> missing value

                           

                            • Re: AppleScript zip-to-mail stops working
                              eskimo Apple Staff Apple Staff (13,905 points)

                              The problem isn't the date. The date is correctly inserted into the outgoing mail.

                              Did you fix it?  Because otherwise I’m confused by your 13 Apr post, where you specifically called out two problems, one of which was the date.

                              *shrug*

                              Anyway, with regards your attachment issue, try the following:

                              set fileToAttach to POSIX file "/Users/quinn/Desktop/tmp.txt"
                              tell application "Mail"
                                  make new outgoing message
                                  set m to the result
                                  tell content of m
                                      make new attachment with properties {file name:fileToAttach} at after the last paragraph
                                  end tell
                              end tell

                              On my machine this creates a message and adds the file as an attachment [1].

                              Share and Enjoy

                              Quinn “The Eskimo!”
                              Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                              let myEmail = "eskimo" + "1" + "@apple.com"

                              [1] It also triggers a TCC alert to grant Script Editor access to the desktop and you have to agree to that.

                                • Re: AppleScript zip-to-mail stops working
                                  rbononno Level 1 Level 1 (0 points)

                                  Okay, I'll try the code you provided and will report back.

                                  Concerning the date. I reported it because if I run the script in the Script Editor and view the log, there is an error message after that line, as in the April 13 message. I do not know why it is there. Could it be that this script predates Sierra?

                                  Thanks.

                                  • Re: AppleScript zip-to-mail stops working
                                    rbononno Level 1 Level 1 (0 points)

                                    Well, I inserted the code you provided and it does insert a zipped file into a mail message, but it ignores the first half of the script and doesn't actually mail the file, which the original script did. If I rearrange the code to insert the send to address and subject line, I get an error message and it doesn't add the attachment.

                                      • Re: AppleScript zip-to-mail stops working
                                        eskimo Apple Staff Apple Staff (13,905 points)

                                        If I rearrange the code to insert the send to address and subject line, I get an error message and it doesn't add the attachment.

                                        I think you’ll have to debug this at your end.  Try each segment of the code in isolation to confirm that it works by itself, and then try assembling these segments, one at a time, until you get to a point where things fail.

                                        Share and Enjoy

                                        Quinn “The Eskimo!”
                                        Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                        let myEmail = "eskimo" + "1" + "@apple.com"

                                          • Re: AppleScript zip-to-mail stops working
                                            rbononno Level 1 Level 1 (0 points)

                                            OK. I'll try that soon as I have some time. Thanks for the assistance.

                                            • Re: AppleScript zip-to-mail stops working
                                              rbononno Level 1 Level 1 (0 points)

                                              Okay! With a bit of fussing, it's working correctly now. However, I'm still getting those nag messages from Mojave about an application that wants to control Mail and then Finder. I have given the script the required privileges in the Automation pane of the Security and Privacy menu, but that doesn't seem to be enough. Does it need to appear Accessibility as well? I'm not sure if I want the script to "control my computer." The script will work but I have to click through the security warnings, both of them.

                                                • Re: AppleScript zip-to-mail stops working
                                                  eskimo Apple Staff Apple Staff (13,905 points)

                                                  I'm still getting those nag messages from Mojave about an application that wants to control Mail and then Finder.

                                                  Those messages should only show up once for any given combination of source and target app.  I suspect what’s happening here is that your script is unsigned, so the system can’t accurately track the source app from run to run.

                                                  You can code sign your script using the File > Export sheet.  If you don’t have a code signing identity, you can get one by using Xcode to set up a Personal Team (just sign in with your Apple ID, you don’t need to join a paid developer programme).

                                                  Share and Enjoy

                                                  Quinn “The Eskimo!”
                                                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                                                  let myEmail = "eskimo" + "1" + "@apple.com"

                                                    • Re: AppleScript zip-to-mail stops working
                                                      rbononno Level 1 Level 1 (0 points)

                                                      Hmmm, okay. Although I don't have Xcode either to my knowledge. I don't have a signing identity. Thanks for the tip though.

                                                      • Re: AppleScript zip-to-mail stops working
                                                        sstanley Level 2 Level 2 (40 points)

                                                        Eskimo said:

                                                        >Those messages should only show up once for any given combination of source and target app.  I suspect what’s happening here is that your script is unsigned, so the system can’t accurately track the source app from run to run.

                                                         

                                                        Signing is not required, though. The problem is that AppleScript apps are self-modifying if the value of any top-level variable changes, and such changes invalidate authorization.

                                                         

                                                        The solution is either to re-write to avoid top-level variables, which usually means putting all the existing top-level code in a handler and calling that, or marking the file execute-only, using something like `chmod a-w` on the applet's executable (/Contents/Resources/Scripts/main.scpt).