I am interested to know the way to reposition a window on the visible screen - not by mouse movement but programmatically.
This is like one of those screen savers you have seen for sure that move around at random positions. I assume one has to know something about screen coordinates and all that stuff but I am lost how to go about it.
It’s easy once you know the dimensions of the screen. Try this:
property display_width : missing value
property display_height : missing value
on idle
my place_randomly(window "main")
return 60
end idle
on place_randomly(the_win)
if display_width = missing value then my get_screen_res()
tell the_win
set {w, h} to size
set position to {random number from 1 to (display_width - w), random number from 1 to (display_height - h)}
end tell
end place_randomly
on get_screen_res()
set display_prefs to do shell script "defaults read /Library/Preferences/com.apple.windowserver DisplaySets"
set display_width to my get_substring(display_prefs, "Width = ", ";") as integer
set display_height to my get_substring(display_prefs, "Height = ", ";") as integer
end get_screen_res
on get_substring(the_string, start_delim, end_delim)
try
set offset_start to (offset of start_delim in the_string) + (length of start_delim)
set the_string to (text offset_start thru -1 of the_string)
set offset_end to (offset of end_delim in the_string) - 1
return (text 1 thru offset_end of the_string)
on error
return the_string
end try
end get_substring
Here’s a quick update on the result of running your “window repositioning” script in Xcode.
I got the script to work until the “do shell script” line. Then, it generated an error like this: "2005-10-21 19:55:13.649 defaults[235] Domain com.apple.windowserverDisplaySets does not exist (1).
I tried to put a “.” between windowserver and DisplaySets but the error won’t go away. I tried a few more variations but no luck.
Then, I decided to hunt for the file in the Library/Preferences directory path you indicated in the code but I did not see the com.apple.windowserverDisplaySets.
I even tied to check almost every plist file in the Preferences and dumped them to see if there is any remote likeness to screen width and screen height parameters. I did not see any. Where could that file be? BTW, I am using Tiger (10.4.2).
I noticed in your message that you had “com.apple.windowserver” and “DisplaySets” put together with no space between them. You should be able to find the file “com.apple.windowserver” in the /Library/Preferences Folder. DisplaySets is a key/value pair in the file. Make sure there is a space between the “com.apple.windowserver” and “DisplaySets” in your script.
Thanks. Actually that option was the first one I tested. I simply pasted Jon’s script which had that name-value pair format but which also generated that AppleScript error.
I went back to the Library/Preferences folder to hunt for the file but still couldn’t find it? Could it be invisible? But why should it be? Could it be in another file? Or is my Tiger (10.4.2) missing something important?
I, too, am using 10.4.2. and the file in question exists on my machine. Just an idea, try going to System Preferences and going to Displays and setting a resolution. I don’t know if that will do anything or not but it’s worth a try.
Personally, I think that reading a plist file for obtaining the screen dimension is kind of a round-about approach. A plist file is a program-dependent document that may or may not always exist. Not that most people will go pitching their windowserver plist, but it could be missing somehow, be corrupt, or not have the correct values you might expect. I think a better method would be to use a cocoa method, which will always provide a valid answer and does not rely on a plist file entry.
Jon has actually already provided the code for this method in another thread.
Jobu was right, I should have put the call method here instead of the plist hack. The complete script should be:
property display_width : missing value
property display_height : missing value
on idle
my place_randomly(window "main")
return 60
end idle
on place_randomly(the_win)
if display_width = missing value then my get_screen_res()
tell the_win
set {w, h} to size
set position to {random number from 1 to (display_width - w), random number from 1 to (display_height - h)}
end tell
end place_randomly
on get_screen_res()
set {x1, y1, display_width, display_height} to call method "frame" of (call method "mainScreen" of class "NSScreen")
end get_screen_res
Just want you to know how happy I am for the great help you guys have provided. I keep learning more and more.
I have actually combined now the randomWindowPos and titleLessWindow codes to make a moving titleless window with definable opacity and background color. I would like to create a simple display for iTunes’ current track or an alarm clock countdown timer that moves around rather than stagnate in one place.
Much credit goes to Jon for the codes and to Jobu for a sprinkling of knowledge-enhancing tips (such as the tutorial on creating custom windows and the comment on plist in this thread).