Saturday, November 27, 2021

#1 2021-11-09 03:44:45 am

jnkendal
Member
Registered: 2006-10-07
Posts: 8

Julian Date and Mail script

Hello there
Some help is needed with the below script. When I run the script, the "current date" &"/" &"msgNo" advances the message number.
The problem is when I save the script as an application, and then open the application, the message number does not advance each time I open the application.
On my previous MacBookPro, I had this script saved on the top tool bar; however, I have forgotten how I managed to get the script on the tool bar.
Any advice would be appreciated.
Regards
Jeffrey

Applescript:

property lastDate : ""
property lastMsgNo : 0

set jDay to do shell script "date +%j"
set jYear to do shell script "date +%y"
set theSubject to jYear & jDay & "/"

set theYEar to do shell script "date +%y" -- two digit year
set theMonth to do shell script "date +%m" -- two digit month
set theMonth2 to (do shell script "date +%B" & " | tr \"[:lower:]\" \"[:upper:]\"")
set theDay to do shell script "date +%d" -- two digit day of month
set theHour to do shell script "date +%H" -- two digit hour (24 hour clock)
set theMinute to do shell script "date +%M" -- two digit minute
set theSecond to do shell script "date +%S" -- two digit second


-- convert the month2 name to a 3 char name
set theMonth2 to characters 1 thru 3 of theMonth2 as string

set currentDate to do shell script "date +%y%j"

if lastDate = "" then -- i.e. first run of script
   set lastDate to jYear & jDay
   
end if

if lastDate = currentDate then
   set lastMsgNo to lastMsgNo + 1
else
   set lastMsgNo to 1
   set lastDate to currentDate
end if

set msgNo to text -2 thru -1 of (("00" & lastMsgNo) as string) -- make 2 digit number

set theSubject to currentDate & "/" & msgNo

set theBody to "DE VJNK NR " & jYear & jDay & "/" & msgNo & return ¬
   & "R " & theDay & theHour & theMinute & "Z " & theMonth2 & theYEar & return ¬
   & "FM KENDAL JN" & return ¬
   & "TO" & return ¬
   & "BT" & return ¬
   & "UNCLAS" & return & return ¬
   & "BT" & return & return & return & return & return & return & return & return ¬
   & "NNNN"

tell application "Mail"
   activate
   set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody}
   tell theNewMessage to set visible to true
   -- Default is false. Determines whether the compose window will
   -- show on the screen or whether it will happen in the background.
end tell

([applescript] and [/applescript] code posting tags added by NG.)

Last edited by Nigel Garvey (2021-11-09 05:17:43 am)

Offline

 

#2 2021-11-09 05:40:09 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6729

Re: Julian Date and Mail script

What version of macOS are you running? App properties don't persist under macOS 11.0 and above.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-11-13 09:33:36 pm

jnkendal
Member
Registered: 2006-10-07
Posts: 8

Re: Julian Date and Mail script

Hello Shane
I am using OS 12.0.1 (Monterey).
Is there a fix/work-around?
Regards
Jeffrey

Offline

 

#4 2021-11-13 10:08:33 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: Julian Date and Mail script

To remember the state of a variable between launches of an application or a script, property list files are used. Although, you can memorize it in a plain text file. You should each time read this variable from this file, then save it back when variable's value changes.

Last edited by KniazidisR (2021-11-13 10:09:32 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-11-13 10:49:59 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6729

Re: Julian Date and Mail script

jnkendal wrote:

I am using OS 12.0.1 (Monterey).
Is there a fix/work-around?



You can use my PrefsStorageLib, available here:

https://latenightsw.com/freeware/


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2021-11-15 07:58:46 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1133

Re: Julian Date and Mail script

When properties stopped working for me, I began using text files to save persistent values. This is easily done and reasonably quick.

When I began using ASObjC, I started using plists to store persistent values. The following saves and returns a list.

Applescript:

use framework "Foundation"
use scripting additions

-- set plist location and name
set preferencesFolder to POSIX path of (path to preferences)
set settingPlist to preferencesFolder & "JulianDateAndMailSetting.plist" -- set to whatever is desired

-- remove comment from following line to write plist
-- writePlist(settingPlist, {1, (current date)}) --> true

-- remove comment from following line to read plist
-- set theSettings to readPlist(settingPlist) --> {1, date "Monday, November 15, 2021 at 9:06:17 AM"}

on readPlist(thePath)
   set theArray to current application's NSArray's arrayWithContentsOfFile:thePath
   return theArray as list
end readPlist

on writePlist(thePath, theList)
   set theArray to current application's NSArray's arrayWithArray:theList
   theArray's writeToFile:thePath atomically:true
end writePlist

Last edited by peavine (2021-11-15 10:26:08 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)