Saturday, December 16, 2017

#1 2017-10-02 11:55:32 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 21
Website

Opening a window in Finder at a given position

I have very simple code aiming to open an existing folder in a Finder window as at set position:

Applescript:

set Folder_Path to "/Users/Shared/"
tell application "Finder"
   open Folder_Path as POSIX file with properties {position:{100, 100}}
end tell

The window opens but Finder ignores the position property requested - instead it opens the window in the middle of the screen or the previously used position.

I'm troubled by the code "as POSIX file" as it's a folder, not a file.  But, I get the same result with:

Applescript:

tell application "Finder"
   open (path to home folder) with properties {position:{100, 100}}
end tell

It does not open in position {100,100}

Can anyone help ?

Thanks.


Public servants are people too

Offline

 

#2 2017-10-03 03:44:11 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Opening a window in Finder at a given position

You may try :

Applescript:

set theFolder to path to home folder

tell application "Finder"
   open theFolder
   set position of Finder window 1 to {100, 100}
end tell

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mardi 3 octobre 2017 10:44:07

Offline

 

#3 2017-10-03 04:56:47 am

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

Re: Opening a window in Finder at a given position

Another way to "open" a window to a folder is to make it. The position property still has to be set separately, but since make returns a result, it can be done in the same line:

Applescript:

set theFolder to path to home folder

tell application "Finder"
   set position of (make new Finder window to theFolder) to {100, 100}
end tell

The to parameter here is an optional one implemented by the Finder and is used to specify either the target of a Finder window or the original item of an alias file.


NG

Offline

 

#4 2017-10-04 10:58:14 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 21
Website

Re: Opening a window in Finder at a given position

Nigel, Yvan, many thanks.

I've tested the alternatives.  They seem to do the same thing - the window opens then moves to the desired location.

Of course, the window has to be open before its properties can be set.   But, it seems not possible to send window properties to the action which opens a window.  Anyway, your approach gets me a window opening where it's needed.


Is there any Apple documentation on the "make" word in AppleScript ?  For example, is it always "make new" ?

Thanks.

Last edited by Neophyte (2017-10-04 11:33:11 pm)


Public servants are people too

Offline

 

#5 2017-10-05 05:05:31 am

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

Re: Opening a window in Finder at a given position

Hi Neophyte.

There doesn't seem to be anything about "make" in the AppleScript Language Guide. It's what's known as an "application command", which means it's not part of the AppleScript core language but is implemented by scriptable applications. It's usually to be found in the "Standard Suite" of their scripting dictionaries, which I think means that the token for it is defined by Apple. But the way it's implemented in any particular application depends on the requirements of the application and how many coffees the developer had while working on that particular version of it. You can't take anything for granted.

In most of the scripting dictionaries I've looked at this morning, 'make' is shown to take a required 'new' parameter and optional 'at', 'with data', and 'with properties' parameters. It may be possible in some cases to leave out the 'new' and make the object class a direct parameter of 'make', but it's best to be consistent and always use 'new'. Script Debugger has a 'with contents' parameter instead of 'with data'. DragThing's 'make' command is in its "DragThing Suite" and doesn't have a 'with data' parameter. The Finder and TextWrangler support an optional 'to' parameter and TextWrangler also has an optional 'initial save location' parameter.

Notwithstanding the optionality of 'at', I've encountered applications which not only insist on it but insist on the location being specified in their own particular way. So, depending on the application and version, you may have to use any of the following formats:

make new aardvark
make new aardvark at aardvarks
make new aardvark at end of aardvarks -- or at beginning of aardvarks, it makes no difference.
make new aardvark at after end of aardvarks
make new aardvark at burrow 1
make new aardvark at after end of aardvarks of burrow 1
etc.

Since scripting dictionaries don't usually elaborate on 'specifier', you just have to experiment (using informed judgement, of course) to see what works for each application. Where an 'aardvark' is an element of a 'burrow' rather than of the application itself, it's obviously necessary to specify the burrow at which the new aardvark will appear.

As you've already discovered, it's not always possible to set certain properties to specific values when using the 'make' command. In such cases, the properties involved have to be set after the item's created.

Welcome to application scripting.  wink


NG

Offline

 

#6 2017-10-05 05:51:06 am

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

Re: Opening a window in Finder at a given position

Nigel Garvey wrote:

There doesn't seem to be anything about "make" in the AppleScript Language Guide.



My printed version, (c) 1993, shows it as:

  make [ new ] className at referenceToLocation [ with properties {propertyLabel:propertyValue [, propertyLabel:propertyValue ]...}] [ with data dataValue ]

That suggests new was meant to be optional, but as you say, it varies by app. The ability for apps to optionally make the at parameter optional was introduced in 10.3.

I vaguely remember a change being made where with data would also take a record or properties, but I might not be right on that.

You can get a bit more idea how it's meant to work by looking up the NSCreateCommand class.


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

Offline

 

#7 2017-10-05 08:18:49 am

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

Re: Opening a window in Finder at a given position

It's the same in my 1997 edition, but application commands are only mentioned in passing in 2008.

Thoughts which come to mind are:

1. Mac OS X was introduced in the intervening years.
2. It's pointless giving syntax in ASLG where developers are free to change it.
3. It takes a very sharp-eyed scripter to notice the difference in syntax bracketing between an optional parameter and a parameter with an optional label — although admittedly it would also take a pretty clueless scripter to try using 'make' without specifying what had to be made!


NG

Offline

 

#8 2017-10-05 08:54:04 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2727
Website

Re: Opening a window in Finder at a given position

Nigel Garvey wrote:

2. It's pointless giving syntax in ASLG where developers are free to change it.



That sums it all up. If we go back to 1993 every standard system supported commands like

Applescript:

open for access new file

new file would show a choose file-like dialog and it would be opened with the open for access command. So if the AppleScript engineers were not consistent, how can anyone expect 3rd party developers will give it an strong syntax?

Offline

 

#9 2017-10-05 06:22:15 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 21
Website

Re: Opening a window in Finder at a given position

Everybody, many thanks for all this.

A few hours after I my last post a friend showed me Make in the Finder dictionary and the light bulb went on for me.  So far in my AS work, I've avoided applications except for Finder and Shane Stanley's excellent Dialog Toolkit.  So, it just didn't occur to me to look in the dictionaries.  AS is actually taking me back over 25 years to the days of AREXX - except that to get a list of application commands in AREXX one had to buy the user manual for each app !

It's truely marvellous for all of you to be so active and responsive to dumbo questions like mine.

Regards.


Public servants are people too

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)