0

I have a script which calculate the timeframe between date and return the result in days or month or years.

For exemple, given myResult = "6 Days" How can I separate the number from the text ?

I managed to get the number only using this :

set myResultNumberOnly to number of myResult

but I can't manage to do the same from the text :

set myResultTextOnly to text of myResult

I also tried

set myResultTextOnly to word of myResult

Maybe I can go with text delimiting? I can't tell the length of the number using this :

set theLength to (get length of (myResultTextNumberOnly as text))

Or should I go with if statement?

How would be the best way to do it?

UPDATE

I just realised that set myResultNumberOnly to number of MyResult give a wrong number, I don't really understand why ?

set MyResult to "Account Value:$478.01"
set myResultNumberOnly to number of MyResult

Result: 21

1

number of MyResult obtains the length of a string. "6 days" just happens to be 6 characters long, whereas "Account Value:$478.01" is 21 characters long.

The most straightforward method to extract numbers from a string would be to split the string into words, then test each item of the list individually to see whether it can be coerced into a number:

set MyResult to "Account Value:$478.01"
set MyResultWords to words of MyResult

repeat with w in MyResultWords
    set w's contents to coerceToNumber(w's contents)
end repeat

set MyResultNumbers to every number in MyResultWords


to coerceToNumber(x)
    local x

    try
        x as number
    on error
        x
    end try
end coerceToNumber

This won't handle string representations of numbers in scientific notation, e.g. 1.234E+09, as the method AppleScript uses to delimit its words results in {"1.234E", "+", "09"}. In this case, you can set text item delimiters to space, and perform the same operation on the text items of My Result.

I have a script which calculate the timeframe between date and return the result in days or month or years.

I do question, however, why your script is producing a result that is a string value of "6 days". It makes me believe the script you are using to calculate time intervals between dates is not doing it in as an amenable way as it could. Partly, this depends on the input your script receives, but in many cases, you should consider converting the input into AppleScript date objects, which then allows you to calculate differences easily and returns to you an actual number that you can manipulate to acquire a measure in days, minutes, hours, etc.

The example script below assumes an ISO 8601-formatted date string as input, i.e. "yyyy-mm-dd", although you could adapt the script to cater for other input formats:

# The units parameter is optional, and defaults to measurements in seconds.
# To choose a different unit, pass one of the constants 'hours', 'days',
# 'minutes', or 'weeks' (without quotes).
on timeInterval from startDate to endDate by units : 1
    tell (current date) to set [startDate, ¬
        day, [year, its month, day]] to ¬
        [it, 1, words of startDate]

    tell (current date) to set [endDate, ¬
        day, [year, its month, day]] to ¬
        [it, 1, words of endDate]

    (endDate - startDate) / units
end timeInterval

timeInterval from "2012-06-03" to "2019-04-01" by days    --> 2493.0
timeInterval from "2012-06-03" to "2019-04-01" by weeks   --> 356.142857142857
timeInterval from "2019-03-31" to "2019-04-01" by hours   --> 24.0
timeInterval from "2019-03-31" to "2019-04-01" by minutes --> 1440.0
timeInterval from "2019-03-31" to "2019-04-01"            --> 8.6E+4 [ = 86400 ]
  • Nice script, so much better than my current script alright, thank you ! – Kevin May 19 at 10:13
  • 1
    As I stated in the description, the input format should be an ISO 8601-formatted date string, i.e. in the format "yyyy-mm-dd". current date returns an AppleScript date object, and "19/05/2019" is not in the "yyyy-mm-dd" format. You are best off writing a second handler to convert dates from "dd/mm/yyyy" to "yyyy-mm-dd", which can be done using words of... and not text item delimiters, i.e. set ISOdate to {word 3, word 2, word 1} of MyDate. – CJK May 19 at 11:13
  • I converted like this and its work fine: set MyDate to short date string of (current date) set [dayVal, MonthVal, YearVal] to the words of MyDate tell (current date) to set MyDate to dayVal & "-" & MonthVal & "-" & YearVal – Kevin May 19 at 11:20
  • Yes, well done—I forgot to concatenate with hyphens. In which case, text item delimiters are appropriate, but your method shown here is perfect. – CJK May 19 at 11:35
  • Don't know what happen, but seems that I did some thing wrong with the conversion apple.stackexchange.com/questions/361347/… – Kevin Jun 1 at 9:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .