Bloopsy Posted February 3, 2018 Share Posted February 3, 2018 So I've been trying to make this workflow for a while but I can't find any answers online and I'm not nearly skilled enough to do it myself. I currently use a few alfred workflows to open certain folders that I use for my work. A lot of the time I need to open more than one of these folders and I usually put them on right side of my screen, one above the other. I've already set up a new workflow to open two finder windows but it just opens two windows on top of each other. This is fine really, but I'd like to make a workflow that would arrange the windows without me doing it. Is there anyway of doing this? Link to comment
deanishe Posted February 3, 2018 Share Posted February 3, 2018 BTW, this question doesn't really have anything to do with Alfred. Alfred is a launcher, but you're asking about application automation. As a rule, Ask Different is a more appropriate place for questions like this. But to answer your question: Yes, it can be done with AppleScript. The AppleScript below will open the folders specified in the theFolders property at the top of the script. By default, it opens each folder as requested, in a new Finder window on the right-hand side of the screen, each below the last. You can run it from Alfred using a Run Script action with Language = /usr/bin/osascript (AS). -- Paths to folders to open property theFolders : {"/Volumes", "/"} -- Set to "true" to open folders on the right-hand side of the screen. property rightAlign : true on getScreenWidth() tell application "Finder" set screenBounds to bounds of window of desktop return item 3 of screenBounds end tell end getScreenWidth tell application "Finder" set yPos to 0 -- vertical position of created windows -- Get width of screen so we can position the windows on the right set screenWidth to my getScreenWidth() -- Open the folders specified above repeat with aFolder in theFolders set theTarget to aFolder as POSIX file set theWin to make new Finder window -- Get size of window and move it to the right-hand side of the screen set theBounds to theWin's bounds set winWidth to (item 3 of theBounds) - (item 1 of theBounds) set winHeight to (item 4 of theBounds) - (item 2 of theBounds) if rightAlign = true then set xPos to screenWidth - winWidth else set xPos to 0 end if set theWin's position to {xPos, yPos} -- Calculate position of next window if yPos = 0 then -- Set yPos to bottom of menubar set theBounds to theWin's bounds set yPos to item 2 of theBounds end if set yPos to yPos + winHeight -- Navigate to the correct folder set theWin's target to theTarget -- Bring Finder to front activate end repeat end tell Link to comment
dfay Posted February 3, 2018 Share Posted February 3, 2018 bonus points for getting @deanishe to write applescript Vero and deanishe 1 1 Link to comment
Bloopsy Posted February 3, 2018 Author Share Posted February 3, 2018 @deanishe Thanks for writing it all out, it's a massive help as I really don't know a thing about AppleScript. I've put the script into Alfred but it's not working as it should. It's opening both windows but they're too tall. The bottom one does start in the right place (right under the top one) but it goes below the screen boundaries. Any ideas why this is happening? Link to comment
deanishe Posted February 3, 2018 Share Posted February 3, 2018 I deliberately designed the script to use your default Finder window size. It would have been a lot easier to specify a size. Do you want to change the default window size or should I add a specific window size to the script? Link to comment
Bloopsy Posted February 3, 2018 Author Share Posted February 3, 2018 I guess I'd prefer to change the default size. I'm confused though as when I open a new finder window it's the size I want, but when I run the script, the windows are much bigger. So I don't think they're using the default size? Link to comment
deanishe Posted February 3, 2018 Share Posted February 3, 2018 My mistake. I just changed my default Finder window size, and the script uses the system default window size, not yours. There doesn't seem to be a way to use the user default Here's an altered version with a hardcoded window size (winWidth and winHeight): -- Paths to folders to open property theFolders : {"/Volumes", "/"} -- Size of created windows property winWidth : 800 property winHeight : 400 -- Set to "true" to open folders on the right-hand side of the screen. property rightAlign : true on getScreenWidth() tell application "Finder" set screenBounds to bounds of window of desktop return item 3 of screenBounds end tell end getScreenWidth tell application "Finder" set yPos to 0 -- vertical position of created windows -- Get width of screen so we can position the windows on the right set screenWidth to my getScreenWidth() -- Open the folders specified above repeat with aFolder in theFolders set theTarget to aFolder as POSIX file set theWin to make new Finder window -- Get size of window and move it to the right-hand side of the screen -- Resize window set {winL, winT, winR, winB} to theWin's bounds set theWin's bounds to {winL, winT, winL + winWidth, winT + winHeight} if rightAlign = true then set xPos to screenWidth - winWidth else set xPos to 0 end if set theWin's position to {xPos, yPos} -- Calculate position of next window if yPos = 0 then -- Set yPos to bottom of menubar set theBounds to theWin's bounds set yPos to item 2 of theBounds end if set yPos to yPos + winHeight -- Navigate to the correct folder set theWin's target to theTarget -- Bring Finder to front activate end repeat end tell Link to comment
Bloopsy Posted February 3, 2018 Author Share Posted February 3, 2018 Thanks so much! This is fantastic. I know this is even more to ask of you, but is there any way to have the windows spaced evenly from the center of the screen? https://imgur.com/a/qfolH Shows basically what I'm looking for. Any thoughts? Link to comment
deanishe Posted February 3, 2018 Share Posted February 3, 2018 (edited) 1 hour ago, Bloopsy said: spaced evenly from the center of the screen? What does that mean? Just vertically? Vertically and horizontally? I'm not going to rewrite the script again after this, so you need to be very clear about what you want. Edited February 3, 2018 by deanishe Link to comment
Bloopsy Posted February 5, 2018 Author Share Posted February 5, 2018 Evenly spaced from the centre of the screen vertically. So like in the diagram I sent, the windows are both the same vertical distance from the centre of the screen. I thought it was clear that they are not evenly space horizontally. Thanks again. Link to comment
deanishe Posted February 5, 2018 Share Posted February 5, 2018 2 hours ago, Bloopsy said: I thought it was clear that they are not evenly space horizontally. They aren't right-aligned with the right edge of the screen, which is where the current script puts them. The implication is that you don't want that behaviour. The boxes appear to be left-aligned with the centre of the screen. The script must assign an X position as well as a Y one. If you don't specify one, then you're stuck with whatever I pick. Link to comment
Bloopsy Posted February 5, 2018 Author Share Posted February 5, 2018 Ah I see, my bad. I meant the graphic to just display the vertical spacing but I should have considered what else it implied. It would be great if you could add a distance from the right edge of the screen also. So just to clarify, the windows are equally spaced vertically from the centre of the screen but also a set distance from the right side of the screen. If this is a pain to do, I would just appreciate them being spaced vertically from the centre if nothing else. Thanks again, I owe you :D. Link to comment
deanishe Posted February 5, 2018 Share Posted February 5, 2018 20 minutes ago, Bloopsy said: If this is a pain to do Not at all. But like I said, I'm not going to keep rewriting the script, so we need to be clear about it's behaviour. Here you go. You can specify the vertical distance to the centre and the horizontal distance to the (left or right) screen edge. -- Paths to folders to open property topFolder : "/Volumes" property bottomFolder : "/" -- Size of created windows property winWidth : 800 property winHeight : 400 -- How many pixels above/below screen centre to position windows property distanceFromCentre : 10 -- How many pixels from side of screen to position windows property distanceFromSide : 50 -- Set to "true" to open folders on the right-hand side of the screen. property rightAlign : true -- Open a directory in a new Finder window on openDirectory(thePath, aboveCentre) tell application "Finder" -- Calculate vertical centre of screen set {screenL, screenT, screenWidth, screenHeight} to bounds of window of desktop set yCentre to screenHeight / 2 -- Create and resize new Finder window set theWin to make new Finder window set {winL, winT, winR, winB} to theWin's bounds set theWin's bounds to {winL, winT, winL + winWidth, winT + winHeight} -- Position window on screen if rightAlign = true then set xPos to screenWidth - (winWidth + distanceFromSide) else set xPos to distanceFromSide end if set theOffset to winHeight + distanceFromCentre if aboveCentre = true then set yPos to yCentre - (winHeight + distanceFromCentre) else set yPos to yCentre + distanceFromCentre end if set theWin's position to {xPos, yPos} -- Open folder in window set theWin's target to (thePath as POSIX file) end tell end openDirectory on run (argv) my openDirectory(topFolder, true) my openDirectory(bottomFolder, false) tell application "Finder" to activate end run dfay 1 Link to comment
Bloopsy Posted February 5, 2018 Author Share Posted February 5, 2018 Awesome, this works exactly like I wanted it to. Thanks for helping out with this! gingerbeardman 1 Link to comment
OmarKN Posted January 19, 2023 Share Posted January 19, 2023 Hi and good day Deanishe, When running this script there was a Finder error: AppleEvent handler failed. the row concerned: set theWin's target to (thePath as POSIX file) these are my paths property topFolder : "/Volumes/Users/okn/Desktop/_Autoconvert" property bottomFolder : "/Volumes/Users/okn/LC3b-wout-Movies/_Pictures-LC3/_autoconverted pics" Otherwise I used the script from the last iteration above, if you could make sense out of it. Would be great! Link to comment
vitor Posted January 19, 2023 Share Posted January 19, 2023 If I understood the request correctly, you should be able to replicate this workflow without code with Automation Tasks. Specifically Files And Folders → Open New Finder Window and Window Management. Link to comment
OmarKN Posted January 20, 2023 Share Posted January 20, 2023 Hi and good day Vitor, As a start I tried to use "Set Custom Window Position," with bounds:x = 15 and y = 85, but something is missing, do you know? This is just for the 1st folder, later there will be 2 more. good bye, OmarKN PS:How to avoid writing in rich text? Link to comment
Stephen_C Posted January 21, 2023 Share Posted January 21, 2023 Set Custom Window Position sets the window position for the topmost window (see the description in the Automation Task). That means your workflow should first open the window you wish to position. Stephen Link to comment
Stephen_C Posted January 21, 2023 Share Posted January 21, 2023 12 hours ago, OmarKN said: PS:How to avoid writing in rich text? Sorry, I overlooked replying to that. I'm afraid that's just the forum software so there's nothing you can do about it. Stephen Link to comment
OmarKN Posted January 21, 2023 Share Posted January 21, 2023 >your workflow should first open the window you wish to position and I thought this was what I was doing: 1. open file/ folder → 2. Set custom window position. ? / with best regards, Omar KN, Stockholm, Sweden Link to comment
vitor Posted January 21, 2023 Share Posted January 21, 2023 Please share the workflow. From a screenshot we’re unable to see how it’s set up. You can use something like transfer.sh for quick hosting. Link to comment
OmarKN Posted January 21, 2023 Share Posted January 21, 2023 (edited) transfer.sh - - xxxcurl: Can't open './Users/okn/LC3b-wout-Movies/Documents-LC3/_Wie_HowTo/w-Alfred5/Workflows1.alfredworkflow'! Possible would be wetransfer.com or dropbox, but then I need an email adr. Otherwise let me just show the ingredients: / with best regards, Omar KN, Stockholm, Sweden Edited January 21, 2023 by OmarKN Link to comment
OmarKN Posted January 23, 2023 Share Posted January 23, 2023 Task Error: Setting window bounds requires macOS Monterey (12) or above. mine is Mac OS 10.14.6 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now