Make AppleScript Open a Path with the Current Users Folder - to Work on Various Computers

I'd like to be able to execute the following AppleScript on any computer.

I think I need to be able to express the users folder as a predetermined variable to use in the script.

mda_server_01 - which is my current user folder

I'd like to be able to run this on any computer and have it work.

The path after (before as expressed in the script) the user folder name is identical on the other computers.

tell application "Finder"
   activate
   make new Finder window
   set bounds of Finder window 1 to {135, 250, 1303, 1200}
   set target of Finder window 1 to folder "Blogs" of folder "Blogs Locations Web Pages Backlinks Press Releases" of folder "MDA" of folder "Muffetta Enterprises" of folder "Muffetta Dropbox" of folder "mda_server_01" of folder "Users" of startup disk
end tell

Copy the actual path of the folder you want and post it.

⌘⌄C

The username will be different on each computer. In this case the user is obviously mda_server_01

On other computers that username will be different.

/Users/mda_server_01/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/Web Pages - Various/

Thank you

I personally would use this syntax, which I refer to as a relative alias:

set folderAlias to ((path to home folder as text) & "Muffetta Dropbox:Muffetta Enterprises:MDA:Blogs Locations Web Pages Backlinks Press Releases:Web Pages - Various:")

But if you prefer to work with POSIX Paths then you can do something like this:

tell application "System Events"
   set folderPath to "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/Web Pages - Various/"
   set folderRef to POSIX path of disk item folderPath as «class furl»
end tell
tell application "System Events"
   set folderPath to "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/Web Pages - Various/"
   set folderRef to POSIX path of disk item folderPath as «class furl»
end tell

tell application "Finder"
   activate
   set newFinderWindow to make new Finder window
   tell newFinderWindow
      set its target to folderRef
      set its bounds to {135, 250, 1303, 1200}
   end tell
end tell

Morning,

In my original script I open one finder window with 4 additional tabs and was able to designate the target of the tabs.

Could you help me finish this new method?
I cannot get the tabs to focus to the correct path in the current version that you are helping me with......

here is my original script

tell application "Finder"
	activate
	make new Finder window
	set bounds of Finder window 1 to {135, 250, 1303, 1200}
	
	set target of Finder window 1 to folder "Blogs" of folder "Blogs Locations Web Pages Backlinks Press Releases" of folder "MDA" of folder "Muffetta Enterprises" of folder "Muffetta Dropbox" of folder "mda_server_01" of folder "Users" of startup disk
	
	delay 0.25
	tell application "System Events"
		tell process "Finder"
			keystroke "t" using command down
		end tell
	end tell
	
	delay 0.25
	set target of Finder window 1 to folder "Web Pages - Various" of folder "Blogs Locations Web Pages Backlinks Press Releases" of folder "MDA" of folder "Muffetta Enterprises" of folder "Muffetta Dropbox" of folder "mda_server_01" of folder "Users" of startup disk
	
	delay 0.25
	tell application "System Events"
		tell process "Finder"
			keystroke "t" using command down
		end tell
	end tell
	
	delay 0.25
	set target of Finder window 1 to folder "Backlinks" of folder "Blogs Locations Web Pages Backlinks Press Releases" of folder "MDA" of folder "Muffetta Enterprises" of folder "Muffetta Dropbox" of folder "mda_server_01" of folder "Users" of startup disk
	set width of column id name column of list view options of Finder window 1 to 755
	
	delay 0.25
	tell application "System Events"
		tell process "Finder"
			keystroke "t" using command down
		end tell
	end tell
	
	delay 0.25
	set target of Finder window 1 to folder "Press Releases" of folder "Blogs Locations Web Pages Backlinks Press Releases" of folder "MDA" of folder "Muffetta Enterprises" of folder "Muffetta Dropbox" of folder "mda_server_01" of folder "Users" of startup disk
	
	delay 0.25
	tell application "System Events"
		tell process "Finder"
			keystroke "t" using command down
		end tell
	end tell
	
	delay 0.25
	set target of Finder window 1 to folder "Muffetta Enterprises" of folder "Muffetta Dropbox" of folder "mda_server_01" of folder "Users" of startup disk
end tell

I appreciate your help...

I think I've got it.
Here is the original new window and an additional tab going to where I want it to.

thank you as always....

tell application "System Events"
	set folderPath to "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/Web Pages - Various/"
	set folderRef1 to POSIX path of disk item folderPath as «class furl»
end tell

tell application "Finder"
	activate
	
	set newFinderWindow to make new Finder window
	tell newFinderWindow
		set its target to folderRef1
		set its bounds to {135, 250, 1303, 1200}
	end tell
end tell

tell application "System Events"
	tell process "Finder"
		keystroke "t" using command down
	end tell
end tell

tell application "System Events"
	set folderPath to "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/"
	set folderRef1 to POSIX path of disk item folderPath as «class furl»
end tell

tell application "Finder"
	activate
	set newFinderWindow to Finder window 1
	tell newFinderWindow
		set its target to folderRef1
	end tell
end tell
1 Like

Hey @troy,

I recommend that people never use Finder-references in AppleScript – i.e.

document file "dumb quote.kmmacros" of folder "Downloads" of folder "theUser" of folder "Users" of startup disk of application "Finder"

These are horrible to read, troubleshoot, and they DON'T WORK outside of the Finder and System Events.

I usually use POSIX Paths outside of the Finder, and when I have to bring references into the Finder I use aliases or fURLs.

-Chris

1 Like

I tried using the script that used to work and got a prompt:

Can’t make «class posx» of «class ditm» "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/" of application "System Events" into type «class furl».

I was running the following that used to work:

tell application "System Events"
	set folderPath to "~/Muffetta Dropbox/Muffetta Enterprises/"
	set folderRef1 to POSIX path of disk item folderPath as «class furl»
	
	set folderPath to "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/"
	set folderRef2 to POSIX path of disk item folderPath as «class furl»
	
	set folderPath to "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/Web Pages - Various/"
	set folderRef3 to POSIX path of disk item folderPath as «class furl»
end tell

any thoughts?

Is the path "~/Muffetta Dropbox/Muffetta Enterprises/MDA/Blogs Locations Web Pages Backlinks Press Releases/ valid?

2 Likes

Hey @troy,

It looks to me like @Nige_S is likely correct about this.

  • Have you rebooted?
  • Have you recopied your folder path and compared to the one in the script?

This handler doesn’t manage your case here, but it's a useful one to have in your toolbox.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2023/05/25 19:19
# dMod: 2023/05/25 19:19 
# Appl: System Events
# Task: Convert a fURL Or Posix Path Or HFS Path Or File Reference to an Alias (Handler).
#     : Handles both Finder file references and System Events file references.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @System_Events, @Convert, @Path, @File, @Reference, @Alias
--------------------------------------------------------

set tildePath to "~/test_directory/test"
set alias1 to convertPathToAlias(tildePath)

set hfsPath to alias1 as text
set alias2 to convertPathToAlias(tildePath)

set theFURL to alias1 as «class furl»
set alias3 to convertPathToAlias(theFURL)

tell application "System Events" to set sevFileRef to disk item "~/test_directory/test"
set alias4 to convertPathToAlias(sevFileRef)

tell application "Finder" to set finderFileRef to file alias1
set alias5 to convertPathToAlias(finderFileRef)

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on convertPathToAlias(thePath)
   tell application "System Events"
      try
         return (path of disk item (thePath as text)) as alias
      on error
         return (path of disk item (path of thePath) as text) as alias
      end try
   end tell
end convertPathToAlias
--------------------------------------------------------
1 Like

ERG!!!! – you mean if I change the folder name...

I'm being sarcastic...

I changed the script to reflect the real folder path and of course it works.

Apologies for my ignorance... I should have seen that... just didn't take the time, as usual.

Thank you so much...