Wednesday, December 13, 2017

#1 2017-10-17 10:49:37 am

roadie
Member
Registered: 2014-05-27
Posts: 9

noob Q - converting a string into a hyphen delimited string + date

Dear MacScripters,
I am learning the applescript with the book AppleScript 1-2-3 and I don’t quite get the concept of ‘as string’ and how to get text out of a variable and change its word delimiters.

I want to ‘patch’ the content of 2 vars, the string “by” and the current short date into one hyphen delimited string that I can use to name a project folder.

Here is what I have so far and I don’t know how to convert the content of titleName to a list so I can apply the hyphen delimiters. I am also looking for a neat way to prepend the current date to the string (yyyy-mm-dd) Any help is much appreciated.
roadie.

Applescript:


---- create project folder name from date-title-author

set titleName to "The Title"
set authorName to "The Author"

set folderSourceName to titleName & space & "by" & space & authorName as string

--> gives me "The Title by The Author"

-- I want to get "yyyy-mm-dd-the-title-by-the-author" as a string


--set AppleScript's text item delimiters to space
--set theseItems to the text items of titleName
--set AppleScript's text item delimiters to {""}
--return theseItems

--set AppleScript's text item delimiters to "-"
--set projectFolderName to {titleName & "by" & authorName} as string
--set AppleScript's text item delimiters to {""}
--return projectFolderName

Offline

 

#2 2017-10-17 12:04:19 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: noob Q - converting a string into a hyphen delimited string + date

First of all the as string coercion in the first line is nonsensical because both names are strings.

Applescript:

set titleName to "The Title"
set authorName to "The Author"

set folderSourceName to titleName & space & "by" & space & authorName

To get year, month and day from current date write

Applescript:

tell (current date) to set {theYear, theMonth, theDay} to {year, its month as integer, day}

its month as integer is necessary because month is an enumerated constant.

Now set text item delimiters (AppleScript's is only necessary in an application tell block) to space and get the text items

Applescript:

set text item delimiters to space
set theseItems to text items of folderSourceName

The result is a list --> {"The", "Title", "by", "The", "Author"}

Put year, month and day at the beginning of the list, here you have to coerce the date integers to string

Applescript:

set theseItems to {theYear as string, theMonth as string, theDay as string} & theseItems

The result is --> {"2017", "10", "17", "The", "Title", "by", "The", "Author"}

Set text item delimiters to a hyphen. Here the as string coercion concatenates the list and puts the delimiters between the items

Applescript:

set text item delimiters to "-"
set finalString to theseItems as string

And finally – very important – reset text item delimiters to the default value to avoid unexpected behavior

Applescript:

set text item delimiters to {""}

Conclusion:

The coercion as string has two meanings: First to convert a type to string (in the example integer to string) and second to join a list of strings to a single string considering the current text item delimiters.

Last edited by StefanK (2017-10-17 12:53:55 pm)


regards

Stefan

Offline

 

#3 2017-10-17 12:42:23 pm

roadie
Member
Registered: 2014-05-27
Posts: 9

Re: noob Q - converting a string into a hyphen delimited string + date

Thank you Stefan for taking the time and explaining this so clearly. Much appreciated.
Invaluable to be able to ask here even if it has been explained a thousand times before.
LG,
roadie

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)