Share your favorite One-Liners! Example:get string offsets in a list.

When you want to do something simple, and maybe only once, without allocating a lot of space in your code, it can be frustrating to build a whole subroutine to handle that one minor task.

Here is a short sub, folded up into a small one-liner so that it fits neatly anywhere that I might need to get an offset of a string from a list.

Do you have any “mini-one-liner-codes” in your tool box?

Do tell!


#
#  HOW TO GET OFFSET(s) OF A STRING WITHIN A LIST using JUST 1 LINE OF CODE
#  by folding up your subroutine into a single line.
#
set lst to {"line 1", "line 2", "line 3", "line 2"}

set srch to "line 2"

set getOFFSETS_oneliner to run script "on run p" & return & "set {hits,n} to {{},0}" & return & "repeat with i in p" & return & "set n to n +1" & return & "if i as text is \"" & srch & "\" then set end of hits to n" & return & "end" & return & "return hits" & return & "end" with parameters lst

return getOFFSETS_oneliner ---------->{2, 4}

(* HERE IS THE 'UNFOLDED' ONE-LINER...

return run script "on run p" & return & ÂŹ
	"set hits to {}" & return & ÂŹ
	"set n to 0" & return & ÂŹ
	"repeat with i in p" & return & ÂŹ
	"set n to n +1" & return & ÂŹ
	"if i as text is \"" & srch & "\" then set end of hits to n" & return & ÂŹ
	"end" & return & ÂŹ
	"return hits" & return & ÂŹ
	"end" with parameters lst
*)


Model: Mac Pro, Yosemite
AppleScript: 2.7
Browser: Safari 601.2.7
Operating System: macOS 10.14

Date calculations can make fun one-liners. :slight_smile:

-- Last day of preceding month.
tell (current date) to set theDate to it - ((its time) + (its day) * days)

-- 15th of preceding month.
tell (current date) to tell it - ((its time) + (its day) * days) to set theDate to it - ((its day) - 15) * days

-- 15th of this month.
tell (current date) to set theDate to it + ((15 - (its day)) * days - (its time))
-- Or:
tell (current date) to set {day, time, theDate} to {15, 0, it} -- NB. This changes the input date.

-- 15th of next month.
tell (current date) to tell it + ((32 - (its day)) * days - (its time)) to set theDate to it + (15 - (its day)) * days
-- Or:
tell (current date) to set {day, day, time, theDate} to {32, 15, 0, it} -- NB. This changes the input date.

-- Tuesday of preceding (Sunday-start) week.
tell (current date) to set theDate to it - ((((its weekday) - (Tuesday)) mod 7 + 7) * days + (its time))

-- Next Tuesday after today.
tell (current date) to set theDate to it + ((((Tuesday) + 6 - (its weekday)) mod 7 + 1) * days - (its time))
-- Or:
tell (current date) to set theDate to it + (((9 - (its weekday)) mod 7 + 1) * days - (its time))

-- Next Tuesday but one after today.
tell (current date) to set theDate to it + ((((Tuesday) + 6 - (its weekday)) mod 7 + 8) * days - (its time))
-- Or:
tell (current date) to set theDate to it + (((9 - (its weekday)) mod 7 + 8) * days - (its time))

-- The date 400 years from now.
tell (current date) to set theDate to it + (1.26227808E+10 - (its time))

That’s great Nigel, I’ll definitely be using those short cuts!
Here is a fun little ‘self reading’ one-liner that lets you select a line from the page you are working on in the Script Editor.


return "You chose:  " & (choose from list ((every paragraph of text of document of window 1 whose first character is "#") as list)) as text

#some comment
#some other comment 

Browser: Safari 605.1.15
Operating System: macOS 10.14

Here is a one-liner for resolving any path beginning with a tilde. (sample included)


set psxPTH to "~/Library/Scripts/"

if psxPTH contains "~" then set psxPTH to ((POSIX path of (POSIX path of (path to home folder))) & (characters (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH) as string)

Browser: Safari 605.1.15
Operating System: macOS 10.14

Ouch! :confused:


set psxPTH to "~/Library/Scripts/"

if psxPTH begins with "~" then set psxPTH to POSIX path of (path to home folder) & text 3 thru -1 of psxPTH

List-to-text coercions should be done with AppleScript’s text item delimiters explicitly set to the delimiter required, which is “” or {“”} here. But the list ‘characters 3 thru -1’ is unnecessary anyway, since ‘text 3 thru -1’ will extract the substring directly.

Addendum: There’s an NSString method in ASObjC which hands things to you on a plate. It requires a use framework “Foundation” statement as well, but if you happened to be working with ASObjC anyway, it would count as a one-liner. :wink:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set psxPTH to "~/Library/Scripts/" -- or even "~yourusername/Library/Scripts/"

set psxPTH to ((current application's class "NSString"'s stringWithString:(psxPTH))'s stringByExpandingTildeInPath()) as text

Thanks Nigel. Shane caught that same blunder for me when I posted that tilde-snip at https://macscripter.net/viewtopic.php?pid=195079. I’m pretty sure you’ve caught me gambling with delimiters before, I may have a bad habit to work on! I’m glad there was a vanilla way to clean it up.

Addendum: In the above mentioned other post, Yvan Koenig cleaned this up by removing the double-posix thing:


set psxPTH to "~/Library/Scripts/"
if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/Library/Scripts/"

But upon further testing I realized it could be confused by inadvertent placement of a tilde elsewhere in the path. So this was a way to restrict the tilde flag to the beginning of the path



--------- while this is ok -------------
set psxPTH to "~/Library/Scripts/"
if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/Library/Scripts/"

--------- this fails -------------
set psxPTH to "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"
if psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/ame~has~a~tilde/"
# NOTE that  the above line got chopped up!

--------- so this is better -------------
set psxPTH to "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"
if character 1 of psxPTH contains "~" then set psxPTH to POSIX path of (path to home folder) & text (2 + (offset of "~" in psxPTH)) thru -1 of psxPTH
return psxPTH ---> "/Users/Mr.Science/Desktop/this~name~has~a~tilde/"

Basically, it can now ignore paths that don’t require the coercion.

Assuming you’re dealing with a path to a file/folder that exists, then:


tell application "System Events" to return the POSIX path of the item named foobar

where foobar can be a posix path (with or without a tilde), an HFS path, an alias object, a POSIX file object, etc.

Very nice CK! Super clean!

Interesting!

named is nominally (!) an optional term which can be used in a name reference or specifier, so these two should be the same:

tell application "System Events" to get the POSIX path of the disk item named foobar
-- Or:
tell application "System Events" to get the POSIX path of the disk item foobar

But the second line errors when foobar’s an alias or a file, so named must be performing some kind of automatic coercion to text:

tell application "System Events" to get the POSIX path of the disk item named foobar
-- Or:
tell application "System Events" to get the POSIX path of the disk item (foobar as text)

Both of the above can also handle one-item lists if the item in each list is a text path or an AppleScript file specifier (eg. set foobar to {(path to desktop) as «class furl»}), but named doesn’t work where the item in a list is an alias.

Following up; I don’t know how effective this is across other releases of OS X but here are four simple one-liners that each convert a tilded path to four other path types. Random samples are used for each run.


set pth to some item of {".", "..", "~", "~/", "~//", "~/Desktop", "~/Desktop/", "~/Desktop//"}

tell application "System Events" to set psxPTH to (POSIX path of (alias pth))
tell application "System Events" to set hfsPTH to path of (alias (POSIX path of (alias pth)))
tell application "System Events" to set alsPTH to (path of (alias (POSIX path of (alias pth)))) as alias
tell application "System Events" to set vrbPTH to alias (path of (alias (POSIX path of (alias pth)))) of application "Finder"

return {"Original path was: ''" & pth & "'' ", return, psxPTH, return, hfsPTH, return, alsPTH, return, vrbPTH, return}

#Sample result:


{"Original path was: ''~/Desktop'' ", "
", "/Users/Mr_Science/Desktop", "
", "MacPro_HD:Users:Mr_Science:Desktop:", "
", alias "MacPro_HD:Users:Mr_Science:Desktop:", "
", folder "Desktop" of folder "Mr_Science" of folder "Users" of startup disk of application "Finder", "
"}

#also posted at https://macscripter.net/viewtopic.php?pid=195637#p195637