Thanks Stephan
however I receveithis error
error “The variable xmlPath is not defined.” number -2753 from “xmlPath”
THanks Yvan
If I removed in Textwrangler the opening “<” and replaced the
with something like
endofcall
would that help at all?
In this case the XML file would become a plain text file … but of course is only a silly guess
Thanks again
Of course the choose file line is missing. I updated my post
Here is a complete script.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
set xmlPath to (choose file) as text
set theDates to {}
tell application "System Events"
set theXMLFile to XML file xmlPath
set callHistoryRecords to XML element "CallHistoryRecords" of theXMLFile
repeat with anElement in XML elements of callHistoryRecords
tell anElement
set xmlDate to value of XML element "date"
set xmlDuration to value of XML element "duration"
set end of theDates to my decipherDatas(xmlDate, xmlDuration)
end tell
end repeat
end tell
set theDates to my concatList:theDates usingString:linefeed
set destFile to ((path to desktop as text) & "theDates_setaDeht.txt")
my writeto(destFile, theDates, «class utf8», false)
#=====
on decipherDatas(xmlDate, xmlDuration)
set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
--> {majorVersion:10, minorVersion:12, patchVersion:1}
tell OSversion to set OSversion to its minorversion as number
-- create the dates from the timestamp
set startDate to (current application's NSDate's dateWithTimeIntervalSince1970:(xmlDate / 1000))
set endDate to startDate's dateByAddingTimeInterval:xmlDuration
-- coerce the dates to AppleScript dates
if OSversion > 10 then
set asStartDate to startDate as date
set asEndDate to endDate as date
else
set asStartDate to my makeASDateFrom:startDate
set asEndDate to my makeASDateFrom:endDate
end if
return date string of asStartDate & tab & date string of asEndDate
end decipherDatas
on makeASDateFrom:theNSDate
set theCalendar to current application's NSCalendar's currentCalendar()
set comps to theCalendar's componentsInTimeZone:(missing value) fromDate:theNSDate
tell (current date) to set {theASDate, year, day, its month, day, time} to ¬
{it, comps's |year|(), 1, comps's |month|(), comps's |day|(), (comps's hour()) * hours + (comps's minute()) * minutes + (comps's |second|())}
return theASDate
end makeASDateFrom:
#=====
on concatList:theList usingString:d1
set anArray to current application's NSArray's arrayWithArray:theList
return (anArray's componentsJoinedByString:d1) as text
end concatList:usingString:
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to POSIX file (POSIX path of targetFile as text)
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 21 décembre 2016 21:26:04
You are right. What really needs to be added is the time to GMT at the date in question, whereas the change I proposed is adding the time to GMT at the time the script is run. In many cases they will probably be the same, but not all.
I don’t think there’s a straight forward way of calculating that, so an ASObjC version should be used.
How may we do the reverse : given an AppleScript date : calculate it’s count of milliseconds since 01/01/1970 ?
I found CFGregorianDateGetAbsoluteTime but it’s described as deprecated.
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 22 décembre 2016 14:51:32
You can subtract two AppleScript dates and get the difference in seconds, then multiply it with 1000
set referenceDateString to "1/1/1970"
set referenceDate to date referenceDateString
set givenDateInMilliseconds to ((current date) - referenceDate) * 1000
Thanks Stefan
It seems that substracting time to GMT is better.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
set referenceDate to date ("1/1/1")
set year of referenceDate to 1970
set theDate to (current date)
set theDateMinus to theDate - (time to GMT)
set givenDateInMilliseconds to (theDate - referenceDate) * 1000
set givenDateInMillisecondsMinus to (theDateMinus - referenceDate) * 1000
# Now use ASObjC to rebuild the ASdates
-- create the dates from the timestamp
set theNSDate to (current application's NSDate's dateWithTimeIntervalSince1970:(givenDateInMilliseconds / 1000))
set theNSDateMinus to (current application's NSDate's dateWithTimeIntervalSince1970:(givenDateInMillisecondsMinus / 1000))
-- coerce the dates to AppleScript dates
set OSversion to (system attribute "sys2") # system attribute belongs to Standard Additions
if OSversion > 10 then
set ASDate to theNSDateMinus as date
set ASDateTooMuch to theNSDate as date
else
set ASDate to my makeASDateFrom:theNSDateMinus
set ASDateTooMuch to my makeASDateFrom:theNSDate
end if
{theDate:theDate, ASDate:ASDate, ASDateTooMuch:ASDateTooMuch}
--> {theDate:date "jeudi 22 décembre 2016 à 16:05:04", ASDate:date "jeudi 22 décembre 2016 à 16:05:04", ASDateTooMuch:date "jeudi 22 décembre 2016 à 17:05:04"}
on makeASDateFrom:theNSDate
set theCalendar to current application's NSCalendar's currentCalendar()
set comps to theCalendar's componentsInTimeZone:(missing value) fromDate:theNSDate
tell (current date) to set {theASDate, year, day, its month, day, time} to ¬
{it, comps's |year|(), 1, comps's |month|(), comps's |day|(), (comps's hour()) * hours + (comps's minute()) * minutes + (comps's |second|())}
return theASDate
end makeASDateFrom:
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 22 décembre 2016 16:06:11
But you still face the same problem: you need to subtract time to GMT for the date in question, which may not be the same as time to GMT when the script is run.