Monday, March 25, 2019
  • Index
  •  » Code Exchange
  •  » Share your favorite One-Liners! Example:get string offsets in a list.

#1 2019-01-20 11:19:57 am

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 39
Website

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!

Applescript:


#
# 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


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#2 2019-01-22 04:51:01 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4823

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

Date calculations can make fun one-liners.  smile

Applescript:

-- 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))

Last edited by Nigel Garvey (2019-01-22 07:09:01 am)


NG

Offline

 

#3 2019-01-30 10:33:43 am

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 39
Website

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

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.

Applescript:


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


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#4 2019-02-15 10:38:12 pm

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 39
Website

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

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

Applescript:


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


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com


Filed under: PATH, POSIX, Hfs, tilde

Offline

 

#5 2019-02-16 03:18:32 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4823

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

Mr. Science wrote:

Applescript:

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)


Ouch!  hmm

Applescript:


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

Applescript:

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

Last edited by Nigel Garvey (2019-02-16 06:29:38 am)


NG

Offline

 

#6 2019-02-17 07:47:56 am

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 39
Website

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

Nigel Garvey wrote:


Ouch!  hmm

Applescript:


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.



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:

Applescript:


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...

Applescript:


--------- 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.

Last edited by Mr. Science (2019-02-17 03:15:46 pm)


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#7 2019-02-25 05:24:25 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 29

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

Mr. Science wrote:

Applescript:

--------- 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:

Applescript:


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.

Offline

 

#8 2019-03-01 01:20:44 am

Mr. Science
Member
From:: Satellite Beach, Florida
Registered: 2015-08-13
Posts: 39
Website

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

Very nice CK! Super clean!


"Fail and fail until you fail to fail!"   ~   http://www.theMrScienceShow.com

Offline

 

#9 2019-03-01 04:46:55 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4823

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

Interesting!

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

Applescript:

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:

Applescript:

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.


NG

Offline

 
  • Index
  •  » Code Exchange
  •  » Share your favorite One-Liners! Example:get string offsets in a list.

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)