Location reference

Hi

I want to create a new script on desktop, but get errors

– with posix path. →Can’t make “/Users/one/Desktop/” into type reference
– HSF path → Can’t get alias “Catalina:Users:one:Desktop:”

Plz suggest, Thank you



tell application "Finder" to set src to (path to desktop) --'s POSIX path
tell application "Google Chrome" to set ChrTitle to title of active tab of front window

tell application "Script Editor"

	make new document  with properties {name:ChrTitle} at begining of src
-- also tried 
--make new document at src with properties {name:newFile}
end tell


Another Try

Now it creates the document, but when compiled error ->The document “ abcd” could not be autosaved. The file doesn’t exist.

is it right to think that the error occurs as the file does not exist?
Cheers


 
tell application "Google Chrome" to set ChrTitle to title of active tab of front window
tell application "Finder" to set src to (path to desktop folder) & ChrTitle &".scpt"

tell application "Script Editor"

   make new document with properties {name:ChrTitle}
save in src

end tell

Hi One208.

-- I assume this line works. I don't have Google Chrome myself.
tell application "Google Chrome" to set ChrTitle to title of active tab of front window

tell application "Script Editor"
	set newDoc to (make new document)
	save newDoc in file ((path to desktop as text) & (chrTitle & ".scpt"))
end tell

You should create in the Script Editor the following script. You can make it in the Script Debugger as well, but without ability to compile it directly. (If you use Script Debugger, you can save Script Editor document only as text, then you need run osacompile command or AsObjC). So, better use Script Editor for your task:


tell application "Google Chrome" to set ChrTitle to title of active tab of front window

-- replace dashes with "-" to fix problem with HFS path
if (ChrTitle contains "/") then set ChrTitle to replaceText(ChrTitle, "/", "-")

-- we can export the Script Editor document only in the existing file (that is, alias)
-- so, we create empty file if it doesn't exist already
try
	set theAlias to alias (((path to desktop folder) as text) & ChrTitle & ".scpt")
on error
	tell application "Finder" -- we use "Finder" to avoid any security restrictions
		set theAlias to make new file at desktop with properties {name:(ChrTitle & ".scpt")}
	end tell
end try

tell application "Script Editor"
	-- make new document, set its contents
	set newScript to make new document with properties {text:"\"Hello, Word!\""}
	-- to save in the compiled form
	compile newScript
	-- can save as "script", "script bundle", "application" and "text"
	save newScript as "script" in theAlias without run only, startup screen and stay open
end tell

on replaceText(mainText, searchText, replaceText)
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, searchText}
	set TIs to mainText's text items
	set AppleScript's text item delimiters to replaceText
	set changedText to TIs as text
	set AppleScript's text item delimiters to ATID
	return changedText
end replaceText

Thanks Nigel for Looking at it

The Script opens a new document, but i am afraid it does not save it

Thank you KniazidisR
the script gives error

That’s probably because of the forward slash in your tab title. Some parts of the system don’t like those in HFS paths. You need replace it with a dash or something else.

on replaceText(mainText, searchText, replaceText)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchText
	set TIs to mainText's text items
	set AppleScript's text item delimiters to replaceText
	set changedText to TIs as text
	set AppleScript's text item delimiters to astid
	
	return changedText
end replaceText

tell application "Google Chrome" to set ChrTitle to title of active tab of front window
if (ChrTitle contains "/") then set ChrTitle to replaceText(ChrTitle, "/", "-")

tell application "Script Editor"
	set newDoc to (make new document)
	save newDoc in file ((path to desktop as text) & (ChrTitle & ".scpt"))
end tell

Hi Nigel, This works very well

Thank you

I have updated the script with the amendment from @Nigel Garvey. The problem is familiar, but somehow I forgot about it.

This seems to work as a way of keeping the original slash(es), if any. It saves the Script Editor document under the doctored name, then renames the file to the original name + “.scpt’” and points the document at it. At first I tried using an alias to the file once it was created in order to save having to preset a post-renaming path. But I was finding in Mojave that if I coerced the HFS path to alias and then used the Finder to rename the file, the alias specifier changed from ‘alias’ + HFS path to ‘alias’ + POSIX path to the unrenamed file! I regard this as aberrant behaviour not common to all systems and so eventually came up with the following:

on replaceText(mainText, searchText, replaceText)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchText
	set TIs to mainText's text items
	set AppleScript's text item delimiters to replaceText
	set changedText to TIs as text
	set AppleScript's text item delimiters to astid
	
	return changedText
end replaceText

tell application "Google Chrome" to set ChrTitle to title of active tab of front window
set scriptName to ChrTitle & ".scpt"
set saveName to replaceText(scriptName, "/", "-")
set savePath to (path to desktop as text) & saveName -- HFS path.
set eventualPath to POSIX path of ((path to desktop as text) & scriptName) -- POSIX path.

tell application "Script Editor" to save (make new document) in file savePath

tell application "Finder" to set name of file savePath to scriptName

tell application "Script Editor" to set path of (first document whose name is saveName) to eventualPath

Hi Fredrik.

If you’re talking about the code in my script, the POSIX path instruction is used to get a POSIX version of the HFS path ((path to desktop as text) & scriptName), which will be needed at the end of the script.

It’s not the same as (POSIX path of (path to desktop)) & scriptName. scriptName has to be included in the POSIX conversion because it potentially contains a slash, which needs to be converted to the POSIX equivalent, a colon: “MacScripter : Location reference.scpt”. This translates back to a slash in the name of the document and in the name of the file as seen on the desktop.

Hi Fredrik.

When I run it in Mojave with this topic as the current tab, it correctly names the file “MacScripter / Location reference.scpt”, but the name of the document is only “Location reference.scpt”. With my version, the name of the file and the document are the same, which was the object of the exercise.

Hi Fredrik.

No problem. It was the slash that was causing KniazidisR’s script and mine to fail when the OP tried them out earlier in the day. One208 was happy with the substitution of a dash, but I thought it would be nice to try to preserve the slash (when one occurs) in both the file name and the name of the still-open document. It’s not a straightforward task! :confused: