AppleScript for Finder stopped working with Mavericks - applescript

I have this very simple AppleScript:
tell application "Finder"
activate
set |Window| to get Finder window 1
set the current view of |Window| to icon view
set |View Options| to icon view options of |Window|
set the icon size of |View Options| to 256
set the label position of |View Options| to bottom
set the shows item info of |View Options| to true
set the shows icon preview of |View Options| to true
set the arrangement of |View Options| to arranged by name
end tell
which worked perfectly well until Mavericks. Ok, it still runs but the desired effect is missing. Nothing, absolutely nothing happens any more.
Anybody knows what changed with the new version of OS X?
Update 1:
Now i noted that the changes do happen — after a Finder restart. So it might (or might not) be related to this question then «Finder update/refresh applescript not working in 10.8» — Only that I did not have a problem with 10.8 it only appeared with 10.9

I simple fix that may help for now, is to add a close window and open window to the script.
tell application "Finder"
activate
set |Window| to get Finder window 1
set the current view of |Window| to icon view
set |View Options| to icon view options of |Window|
set the icon size of |View Options| to 256
set the label position of |View Options| to bottom
set the shows item info of |View Options| to true
set the shows icon preview of |View Options| to true
set the arrangement of |View Options| to arranged by name
set flipTarget to folder "Users" of startup disk
set targ to target of |Window|
set target of |Window| to flipTarget
set target of |Window| to targ
(* --close |Window|
--open targ
*)
end tell
Not ideal, but until someone works out how to solve this bug. And I call it a bug for want of a better explanation. It may do.
The script collects the target of window one. Closes window 1. Then opens the target of what was window 1.
I suspect this bug is related to the fact that in Mavericks if you open up an applications preference plist file and make a change. The change may not take effect as they used to do in pre 10.9. I think this is because of a change to how the preferences are read and when they are read. I t seems that what is in the memory will take presidents over manual changes. They do however change straight away if you use the unix command defaults.
***UPDATE*1
In Martin's answer there is a good idea of just flipping the target.
But with the problem of not working on a root directory.
The simple answer to that is to use a specific flip target. In this cae the users home folder. We all have on of those..
I have update the last part of the code and comment out the old bit.
The change code is
set flipTarget to folder "Users" of startup disk
set targ to target of |Window|
set target of |Window| to flipTarget
set target of |Window| to targ

#markhunte is right – it seems to be a bug and one needs to reopen the window or similar to get around it. On macscripter.net I found some additional infos. My current version is now (learned other new tricks as well):
tell application "Finder"
activate
tell Finder window 1
set current view to icon view
set its icon view options's properties to {icon size:64, label position:bottom, shows item info:true, shows icon preview:true, arrangement:arranged by name}
-- we refresh the window to reflect the icon size change!
set Original_Target to its target as alias
set Parent_Target to container of its target as alias
set target to Parent_Target
set target to Original_Target
end tell
end tell
This solution does not need to close the window but only changes its target. In the script editor you see the window flashing — but when started from the script menu it is so fast you don't notice any more.
Disadvantage of this solution over #markhunte solution: Won't work on the root directory.

Related

Swap between safari windows in a ninja way

The scenario I have is that the new windows opens, and the script executes inside it. I am looking for a way how to come back to the previous active window and did try these 2 ways:
1. Executing keyboard shortcut
tell application "System Events" to keystroke "§" using command down
Note: Unpleasant experience since the slight flashback as a result of window swap.
2. Giving lastWindow personal ID and later bringing it to the front
set lastWindow to id of window 1
...
set index of window id lastWindow to 1
Note: When the lastWindow is changed it becomes visible but inactive state, and requires an additional click on page to make it truly active
I did also try to change visible false of the newly created window, but it results in making it minimized and slowing the speed of background script execution.
Question. So is there a way to create new window and swap back to the last one while in a most "silent" way?
In reference to:
Note: When the lastWindow is changed it becomes visible but inactive state, and requires an additional click on page to make it truly active
The following example AppleScript code will raise the previous front most window to the top having full focus and be active. No additional click necessary.
tell application "Safari"
activate
set thePreviousFrontWindowID to id of front window
make new document with properties {URL:"https://www.google.com"}
delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
set index of window id thePreviousFrontWindowID to 1
end tell
tell application "System Events" to perform action "AXRaise" of front window of application process "Safari"
It's the last line in this example AppleScript code that you need to get the window all the way to the top fully focused and active.
In reference to:
Question. So is there a way to create new window and swap back to the last one while in a most "silent" way?
Since windows don't make any sound when, for example, running the example AppleScript code above it couldn't be any more "silent" however, as far as minimizing visual distractions, that's so subjective that it almost should not have been asked. The fact is, there is always going to be some level of visual distraction as one moves though the windows of an app, manually or programmatically.
The only suggestion I have is, maybe set the bounds of the new window to that of the previous front window. Then when whatever is happening in the new and current front window is done and you bring the previous front window forward, you will see the entire window and none of the new window that's now behind it.
tell application "Safari"
activate
set thePreviousFrontWindowID to id of front window
set thePreviousFrontWindowBounds to bounds of front window
make new document with properties {URL:"https://www.google.com"}
set bounds of front window to thePreviousFrontWindowBounds
delay 4 -- # The delay command is here as a placeholder for whatever you're doing in the new window.
set index of window id thePreviousFrontWindowID to 1
end tell
tell application "System Events" to perform action "AXRaise" of front window of application process "Safari"
These are just examples to show how to get farther along then you were.

Using Applescript to Change desktop icon size

I am currently using applescript to setup various parameters of the desktops at my university. So far my script successfully changes the desktop background and the dock size.
The problem at hand is when I run the script, majority of the time, the icons on the desktop never change.
Here is the script I wrote to alter the desktop icon's size and grid spacing:
tell application "System Events"
set finderPrefsFile to property list file "~/Library/Preferences/com.apple.Finder.plist"
tell finderPrefsFile
tell property list item "DesktopViewSettings"
tell property list item "IconViewSettings"
set value of property list item "gridSpacing" to "100"
set value of property list item "iconSize" to "32"
set value of property list item "arrangeBy" to "none"
end tell
end tell
end tell
end tell
#Restart Finder for changes to take effect.
do shell script "killall Finder"
How should I go about altering the script to make it work all the time (I would like to eventually share this script with some of my classmates).
P.s.
Here is the full script
I didn't get your script work reliable. I played around with timeouts but didn't get the Finder to refresh using the new settings.
But I found to set some view options directly using vanilla AppleScript:
tell application "Finder"
activate
set iconViewOptions to desktop's window's icon view options
tell iconViewOptions
set arrangement to not arranged
set icon size to 32
set shows item info to false
set shows icon preview to false
end tell
quit
end tell
delay 1
tell application "Finder" to activate
The AppleScript quit-handler works more reliable then do shell script "killall Finder", maybe the killall is too hard...
The delay 1 does the magic to give the Finder the time to breath before get up again and using it the Script works each time...
But one thing is AFAIK not possible in Finder scripting: Setting the grid space :-/
Greetings, Michael / Hamburg

javafxpackager Positions Applications Folder Incorrectly On Mac OS X

I'm using Mac OS X Mavericks and JDK 1.7.45 to package a Swing application using the javafxpackager tool for Mac. Unfortunately the end result looks like this (hid the icon/name):
Scrolling a bit to the right clarifies the picture:
Apparently the Applications image was placed incorrectly within the DMG. I have no idea how that can happen or what I might have done that triggered this, I just used the standard NetBeans packaging script. Any hints or directions would be appreciated.
OK this turned out to be rather simple, it seems that the tool generates the script:
tell application "Finder"
tell disk "MyApplication"
open
set current view of container window to icon view
set toolbar visible of container window to false
set statusbar visible of container window to false
-- size of window should match size of background
set the bounds of container window to {400, 100, 917, 370}
set theViewOptions to the icon view options of container window
set arrangement of theViewOptions to not arranged
set icon size of theViewOptions to 128
set background picture of theViewOptions to file ".background:background.png"
-- Create alias for install location
make new alias file at container window to POSIX file "/Applications" with properties {name:"Applications"}
-- First, move all files far enough to be not visible if user has "show hidden files" option set
-- Note: this only make sense if "hidden" files are also visible on the build system
-- otherwise command below will only return list of non-hidden items
set filesToHide to the name of every item of container window
repeat with theFile in filesToHide
set position of item theFile of container window to {1000, 0}
end repeat
-- Now position application and install location
set position of item "MyApplication" of container window to {120, 135}
set position of item "Applications" of container window to {390, 135}
close
open
update without registering applications
delay 5
end tell
end tell
Which is problematic since it only delays for 5 (I assume seconds) so I increased this to 20 and it worked. The script should be placed under package/macosx/MyApplication-dmg-setup.scpt.

How can I tell Finder to make a new Finder window with properties?

I have the following snippet in a larger script:
--I think this should work…
--make new Finder window with properties {target:theWallpaperPosixFile}
set theWindow to make new Finder window
set target of theWindow to theWallpaperPosixFile
Why does the commented out line not work when I believe it should be functionally identical to the bottom line?
EDIT: To be clear, the code works as is. I'm missing something about with properties. My understanding from this as well as many other sites and the Finder Dictionary is that the 2 liner should be equivalent to the one liner. But it's not. It just pops open a new Finder window with no target.
So my question is specifically how to use with properties, not 'how to make it work'.
Try:
set wallpaperPaths to {POSIX path of (path to documents folder)} -- Example
repeat with wallpaperPath in wallpaperPaths
set theWallpaperPosixFile to POSIX file (contents of wallpaperPath) as alias
tell application "Finder" to set theWindow to make new Finder window to theWallpaperPosixFile
end repeat
EDIT
Look in the dictionary for Make in the Standard Suite:

Set bounds of Screen Sharing window no longer working in Lion

I use my Mac Mini to play movies and music on while working on my MBP. I like to keep screen sharing open but reduced in size in the upper right corner of my screen so that I can effectively have a "PIP". When I want to tag a song that is playing or change the movie, I use a key command to make the window full size, then another key command to shrink it back and position it in the upper right corner.
Since I updated to Lion, I've been getting the following error:
error "Screen Sharing got an error: Can’t set window 1 to {1191, 22,
1441, 185}." number -10006 from window 1
The code is below. Does anyone know what I'm doing wrong?
tell application "Screen Sharing"
activate
set the bounds of the first window to {1191, 22, 1441, 185}
end tell
I have also tried changing the code to use the wording "set the bounds of window 1..." but get the same error.
Any help would be much appreciated.
--Adam
If you look at the applescript dictionary for the application, it doesn't know those commands (e.g.. window or bounds). It only knows the GetURL command so it certainly won't work.
However there is another option. System Events knows about windows and it knows the size and position commands. So you can use that instead...
tell application "System Events"
set ssProcess to first process whose name is "Screen Sharing"
tell ssProcess
tell first window
set position to {0, 20}
set size to {605, 400}
end tell
end tell
end tell
tell application "Screen Sharing" to activate

Resources