Jump to content
fns720

Focus on AppleScript "choose file name" dialog

Recommended Posts

I made a simple workflow to save a URL as a .webloc file - here's the relevant part:

tell application "Finder"
	activate
	make new internet location file at filePath to theURL with properties {name:fileName}
end tell

Everything runs properly except that the "choose file name" dialog looses its focus. Unfortunately I have no idea how to set the focus to the dialog from script. Any suggestions?

 

Share this post


Link to post

try something like this:

tell application "Finder"
   activate
   display dialog "Give the file name:" default answer ""
   set theName to text returned of result
   
   set webloc to make new internet location file to theurl at desktop with properties {name:theName, location:theurl}
end tell

Share this post


Link to post

I want to see the "choose file name" dialog because I want to assign Mavericks tags to the .webloc file - so a simple "display dialog" requester is not enough for me :(.

Share this post


Link to post


You may have to reference the application's save dialog ... because that is generally where they are.

Putting the following into the applescript editor brings up the dialog I think you want.


set resultFile to (choose file name with prompt "Save As File" default name "My File" default location path to desktop) as text if resultFile does not end with ".txt" then set resultFile to resultFile & ".txt"


Edited by RodgerWW

Share this post


Link to post

Thanks for your help Rodger. Here's the entire workflow script:

on alfred_script(q)

set defaultPath to "SYSTEM:Users:fns:Dropbox:URL" as alias
tell application "Safari" to set theName to name of front document
tell application "Safari" to set theURL to URL of front document
set theName to replace_chars(theName, ":", " -")

set text item delimiters to ":"
set fileName to last text item of resultFile
set filePath to text 1 thru lastpos(resultFile, ":") of resultFile

set resultFile to (choose file name with prompt "Save URL" default name theURL default location filePath) as text
if resultFile does not end with ".webloc" then set resultFile to resultFile & ".webloc"

tell application "Finder"
	make new internet location file at filePath to theURL with properties {name:fileName}
end tell

end alfred_script

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on lastpos(textstring, thischar)
	return (length of textstring) - (offset of thischar in (reverse of text items of textstring as string))
end lastpos

Share this post


Link to post

I can get this to work by moving two 'sets' after the dialog as such:

on alfred_script(q)

set defaultPath to "SYSTEM:Users:fns:Dropbox:URL" as alias
tell application "Safari" to set theName to name of front document
tell application "Safari" to set theURL to URL of front document
set theName to replace_chars(theName, ":", " -")

set text item delimiters to ":"

set resultFile to (choose file name with prompt "Save URL" default name theURL default location filePath) as text
if resultFile does not end with ".webloc" then set resultFile to resultFile & ".webloc"

set fileName to last text item of resultFile
set filePath to text 1 thru lastpos(resultFile, ":") of resultFile

tell application "Finder"
    make new internet location file at filePath to theURL with properties {name:fileName}
end tell

end alfred_script

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

on lastpos(textstring, thischar)
    return (length of textstring) - (offset of thischar in (reverse of text items of textstring as string))
end lastpos

On MY system, I changed the default path variable ... but, Loaded a site in Safari, triggered the script and it saved the webloc into my documents folder with the dialog popping up in front of Safari so I could tag it.

Share this post


Link to post

Thanks Rodger, now it works:

on alfred_script(q)

set defaultPath to "SYSTEM:Users:fns:Dropbox:URL" as alias
tell application "Safari" to set theName to name of front document
tell application "Safari" to set theURL to URL of front document
set theName to replace_chars(theName, ":", " -")

tell application "Safari"
	activate
	set resultFile to (choose file name with prompt "Save .webloc File" default name theName default location defaultPath) as text
	-- if resultFile does not end with ".webloc" then set resultFile to resultFile & ".webloc"
end tell

set text item delimiters to ":"
set fileName to last text item of resultFile
set filePath to text 1 thru lastpos(resultFile, ":") of resultFile
    
tell application "Finder" to make new internet location file at filePath to theURL with properties {name:fileName}
 

end alfred_script on replace_chars(this_text, search_string, replacement_string) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars on lastpos(textstring, thischar) return (length of textstring) - (offset of thischar in (reverse of text items of textstring as string)) end lastpos

Edited by fns720

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...