I’ve been trying to find some info on setting the position of the vertical scrollbar in a given finder window, however I’m not sure if I’ve found anything of use. I’ve also had a look through the Finder dictionary to see if I can find anything, once again I’m a little lost.
As mentioned earlier all I’d like to do is open a particular folder window, which isn’t problem, then set the position of the vertical scrollbar so certain files are visible.
Please can someone point me in the right direction.
To do something like this, Nick, you’ll need UI elements to be enabled on your machine - so, just in case…
tell application "System Events" to set UI elements enabled to true
The following routine should set the vertical scrollbar (if it exists) of the frontmost Finder window (if it exists) to halfway. Simply adjust the input value (currently set to 0.5) to any number between 0.0 and 1.0:
to |vscroll Finder window| to v
tell application "System Events" to tell window 1 of application process "Finder"
if not (exists) then return
set t to it
tell t's splitter group 1 to if exists then set t to it
tell (scroll bar 1 of t's scroll area -1 whose orientation is "AXVerticalOrientation") to if exists then set value to v
end tell
end |vscroll Finder window|
|vscroll Finder window| to 0.5 (* number between 0.0 and 1.0 *)
I’ve had a play with the code and it works great, thanks again for the help.
One thing I have been trying to do is take Kai’s solution a little further to get the positional value of the vertical scrollbar and put it in a variable. The code so far is listed below:-
tell application "System Events" to set UI elements enabled to true
to |vscroll Finder window| to v
tell application "System Events" to tell window 1 of application process "Finder"
if not (exists) then return
set t to it
tell t's splitter group 1 to if exists then set t to it
-- ORIGINAL CODE
--tell (scroll bar 1 of t's scroll area -1 whose orientation is "AXVerticalOrientation") to if exists then set value to v
-- New code to get value of vertical scrollbar position
-- into variable barPosition
tell (scroll bar 1 of t's scroll area -1 whose orientation is "AXVerticalOrientation") to if exists then set barPosition to value
-- Display the position of the vertical scrollbar
display dialog barPosition
end tell
end |vscroll Finder window|
|vscroll Finder window| to 0.5 (* number between 0.0 and 1.0 *)
When I execute the code it appears to be working however sometimes the values don’t appear to correspond correctly with the position of the scroll bar.
Can anyone point out why that might be?
I tried to turn the code round a little to get the value I require however I’m not sure if that’s correct. I’d also like to try and limit the values I’m getting to 2 decimal places, I should be able to find something for that though.
Edit: Ah - glancing up, I see that your reply just beat mine.
Certainly if the main point of the exercise is to see a certain file, Nick, then j’s suggestion is an excellent one. That way, even if the relative position of the file changes within the window, it will still be revealed.
(Incidentally, in earlier versions of Mac OS, Finder’s reveal and select commands behaved somewhat differently - but they now seem to do pretty much the same thing. :/)
Another benefit of j’s approach is that you wouldn’t have to explicitly open the window in question. Finder would take care of that for you, too.
You could also, if required, specify more than one file - and Finder would attempt to show them all (as far as possible, within the confines of the window’s bounds). If you didn’t want the file(s) to remain selected, then the following variation shows how you might deselect it/them:
set filename_list to {"file name A.txt", "file name B.txt"} (* modify as required *)
set folder_path to "path:to:target:folder:" (* modify as required *)
tell application "Finder"
select (folder folder_path's items whose name is in filename_list)
select {} (* optional *)
activate (* optional *)
end tell
Perhaps I should have explained what I was trying to do ultimately.
I’m trying to create a script that remembers the scrollbar positions of a given finder window at the click of a button. The script would therefore just remember the scrollbar positions rather than reveal a particular file or folder. J’s approach is a nice one and works fine, I was just trying to do it from a different approach. I hope that makes sense.
Can’t keep up with you, Nick - you always seem to keep a message ahead. (Ahem… to follow this discussion, folks, read the odd-numbered messages first - then follow up with the even numbered ones…)
Not entirely sure, Nick - seems to work fine here. Can you figure out under what conditions the anomaly occurs, so that we could possibly replicate it - or is it pretty much random?
Yeah - below.
Just use something like:
tell (scroll bar 1 of t's scroll area -1 whose orientation is "AXVerticalOrientation") to if exists then set barPosition to value div 0.01 / 100
My fault entirely, Nick - constantly scurrying off to keep one or two clients quiet. (But then I suppose they do help to pay a few of the bills…)
Excellent.
While I obviously don’t know exactly how you intend to approach this, I’ve cobbled together the effort below - in the hope that it might be remotely useful to you.
When the script is invoked, it should check the vertical scrollbar settings for a range of windows (whenever they’re frontmost), and remember them in future. If the current front window hasn’t yet been encountered, you’ll be asked if you want to store its setting. The script should also offer politely to step aside for a few moments - just in case you wish to adjust the scrollbar position before storing its position.
Since the dialog is called by the application running the script, and not by Finder, you could even leave it open - and then return to it after having made made your adjustments. (Note that, if you switch Finder windows while the dialog is up, the original window will remain the script’s target.)
If, on the other hand, the window is a previous acquaintance, then the stored setting is reapplied to the scrollbar. (To clear the settings in memory, simply recompile the script.)
Anyway, hope it helps in some small way…
property target_list : {}
property value_list : {}
property current_target : missing value
property current_scrollbar : missing value
to |store scrollbar|()
set target_list's beginning to current_target (* set beginning, rather than end, for faster access to recent targets *)
tell application "System Events" to set value_list's beginning to (value of current_scrollbar) div 0.01 / 100
end |store scrollbar|
to |restore scrollbar|()
repeat with i from 1 to count target_list
if current_target is my target_list's item i then
tell application "System Events" to set current_scrollbar's value to value_list's item i
exit repeat
end if
end repeat
end |restore scrollbar|
to |get current scrollbar|()
tell application "System Events"
set t to window 1 of application process "Finder"
tell t's splitter group 1 to if exists then set t to it
tell (scroll bar 1 of t's scroll area -1 whose orientation is "AXVerticalOrientation")
if not (exists) then error number -128 (* don't bother to continue *)
set current_scrollbar to it
end tell
end tell
end |get current scrollbar|
to |ask user| to do_something
if button returned of (display dialog "Store the vertical scroll position of the front Finder window?" & return & return & "If necessary, click Wait., make any adjustments - and then rerun the script." buttons {"Cancel", "Store Now", "Wait..."} default button do_something with title "Store Scrollbar" with icon 1) is "Store Now" then |store scrollbar|()
end |ask user|
tell application "Finder" to tell Finder window 1
if not (exists) then return (* don't bother to continue *)
set this_target to target as alias
end tell
|get current scrollbar|() (* check that a scrollbar exists - even if the target is the same as before *)
if this_target is in target_list then
set current_target to this_target
|restore scrollbar|()
else if this_target is current_target then
|ask user| to "Store Now"
else
set current_target to this_target
|ask user| to "Wait..."
end if
No matter. The problem clearly occurs in the Finder tell statement, right at the start of the run handler. So here are a few things to try - preferably in the given order. (You can test the following snippets individually, rather than within the main script…)
The part that concerns us currently looks like this:
tell application "Finder" to tell Finder window 1
if not (exists) then return (* don't bother to continue *)
set this_target to target as alias
end tell
First, try running this slightly tweaked version:
tell application "Finder" to tell Finder window 1
if not (exists) then return
set this_target to its target as alias
end tell
If that doesn’t work, try this:
tell application "Finder" to tell Finder window 1
if not (exists) then return
set this_target to its folder as alias
end tell
And if that’s no good, here’s a last resort:
tell application "Finder" to tell window 1
if not (exists) then return
set this_target to its folder as alias
end tell
Since that last version worked beautifully in Mac OS 9 (and probably way before that), I’ll be a little disappointed if we can’t get it to work for you, too. Fingers crossed…
WOW, fantastic! That’s just what I was looking for!
I tried the code snippets and they all worked! ???
I got the ‘result’ alias “Jobs on PRGY_PRIME F:”
After running each of the snippets I then tried running the original code and that worked fine. I can’t figure out why it didn’t work yesterday, I haven’t done anything with the script and the mac hasn’t been restarted???
Anyway, thanks ever so much for your help with this, it’s greatly appreciated.