Sunday, September 27, 2020

#1 2020-02-06 08:56:56 pm

johnmcald
Member
Registered: 2018-07-23
Posts: 5

Setting A Position Property From A String

Hi all, first-time poster, long-time reader...

I'm having an issue figuring out how to set a position property from a text string.
I have a plist that I have written the position of a Finder window to. When writing to the plist I converted the position property (that looked something like {900,900}) to a string (which looks something like "{900,900}") to save it as text to the plist item.

I now want to pull the text from that plist item to use it to change the position of a window. But since it's a string I have to find a way to convert it to a property??? This the bit that's confusing me. I understand that it's not working because the string I'm getting from the plist has quotes around it and I have to somehow find a way to remove the quotes so that the position property sees it as a valid value.

So, I tried many variations for compiling the value for the position so that it doesn't have quotes but no matter what I do I can't seem to figure it out.

Example script:

Applescript:


set thePosition to "{900,900}" --this is how it is returned from the plist

tell application "Finder"
set theTarget to ":Users:Shared:" as alias
set newWindow to make new Finder window to theTarget

set position of newWindow to thePosition --this is where it gets hairy because I have a quoted form of a value for a property position. and it shouldn't be quoted. and it's not really a quoted form, it's just text.

end tell

Thanks for look and I look forward to reading your posts.

JM

Offline

 

#2 2020-02-06 09:37:19 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Setting A Position Property From A String

Applications that read and store positions in property lists generally use two functions, NSPointFromString() and NSStringFromPoint(). To use them in AppleScript you need a suitable use framework statement, like this:

Applescript:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use scripting additions

set thePosition to "{900, 900}"
set thePos to current application's NSPointFromString(thePosition)
--> {x:900.0, y:900.0}

The result is a record, so you can ask for "x of thePos" or "y of thePos".


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-02-07 02:58:01 am

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

Re: Setting A Position Property From A String

And yet another method for your armoury:

Applescript:

set thePosition to "{900,900}" --this is how it is returned from the plist
set thePosition to (run script thePosition)
--> {900, 900}


NG

Offline

 

#4 2020-02-07 07:12:27 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1305

Re: Setting A Position Property From A String

Nigel,

I deleted my proposal, as you found much better plain AppleScript solution. smile


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-02-07 08:08:22 am

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

Re: Setting A Position Property From A String

KniazidisR wrote:

I deleted my proposal, as you found much better plain AppleScript solution. smile


Hi KniazidisR.

You needn't have. Your solution was somewhat more efficient performancewise.  smile

Applescript:

set thePosition to "{900,900}" --this is how it is returned from the plist

set o to (offset of "," in thePosition)
set x to (text 2 thru (o - 1) of thePosition) as integer
set y to (text (o + 1) thru -2 of thePosition) as integer
set thePosition to {x, y}
--> {900, 900}


NG

Offline

 

#6 2020-02-07 04:33:59 pm

johnmcald
Member
Registered: 2018-07-23
Posts: 5

Re: Setting A Position Property From A String

Thanks for all of the support. I ended up using Nigel's method since it was the easiest but I tested everyone's to see how they worked. (and they did...)

I'm very interested in Shane's thought of using foundation "Framework." I've used this once before but on a recommendation and I didn't understand why I was using but after Shane posting about it I've grown a bit more curious about it and did some digging to find that you can use regular expressions (something I've been hunting for and can't believe I never found this). I will definitely do more digging on this front.

Thanks.

Offline

 

#7 2020-02-07 04:55:34 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Setting A Position Property From A String

FWIW, you can do regular expressions using my RegexAndStuffLib script library, available here:

https://www.macosxautomation.com/apples … _Libs.html

It uses Foundation, but keeps all that side of things out of sight, so you can write stuff like this:

Applescript:

use scripting additions
use script "RegexAndStuffLib" version "1.0.6"

regex change "one two three" search pattern "t(.?)" replace template "t**$1**" --> "one t**w**o t**h**ree"


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2020-02-07 10:22:16 pm

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

Re: Setting A Position Property From A String

Here's another one:

Applescript:

tell application id ("com.apple.Finder")
               tell (make new Finder window to continue ¬
                               path to shared documents folder)
                               set the position to words of "{900, 900}"
               end tell
               activate
end tell

The key part to highlight is:

Applescript:

words of "{900, 900}"

which works well in this case because the parts of the string you want to extract are clearly word-like, whilst the parts that aren't needed happen to be non-word characters.  It returns a list containing the two words, namely {"900", "900"}.  Despite being a list of strings rather than a list of numbers, AppleScript thankfully has a small amount of intuition about what to do and, somewhere along the lines, turns  what it receives into the appropriate data type (which is of a class called point whose superclass is a list, but specifically a list containing precisely two numerical values).  Explicitly:

Applescript:

return {"900","900"} as point --> {900, 900}

Offline

 

#9 2020-02-07 10:54:49 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6460

Re: Setting A Position Property From A String

CK wrote:

a class called point whose superclass is a list, but specifically a list containing precisely two numerical values).



Technically it's not actually a subclass of list, but a type of its own (historically based on a QuickDraw point), although it usually gets converted to a list. But it has some limitations lists don't have: integers only, and a limited range of values.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2020-02-07 11:29:45 pm

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

Re: Setting A Position Property From A String

Shane Stanley wrote:
CK wrote:

a class called point whose superclass is a list, but specifically a list containing precisely two numerical values).



Technically it's not actually a subclass of list, but a type of its own (historically based on a QuickDraw point), although it usually gets converted to a list. But it has some limitations lists don't have: integers only, and a limited range of values.

thumbs up
Thank you.

Offline

 

#11 2020-02-09 10:09:58 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 620

Re: Setting A Position Property From A String

An alternative which demonstrates the potential speed advantage of text item delimiters:

Applescript:

set thePosition to "{900,900}"
set text item delimiters to {"{", ",", "}"}
set thePosition to {text item 2 of thePosition as integer, text item 3 of thePosition as integer}
set text item delimiters to ""
thePosition --> {900, 900}

Script Geek timings of the scripts in this thread:

Shane: 0.9 millisecond
Nigel: 1.3 millisecond
KniazidisR: 1.3 millisecond
Peavine: 0.3 millisecond

Last edited by peavine (2020-02-10 08:09:48 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)