Sunday, October 17, 2021

#1 2021-09-16 01:55:51 am

One208
Member
Registered: 2021-03-15
Posts: 32

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

Applescript:



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

Offline

 

#2 2021-09-16 04:03:15 am

One208
Member
Registered: 2021-03-15
Posts: 32

Re: Location reference

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

Applescript:



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

Offline

 

#3 2021-09-16 04:04:11 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5427

Re: Location reference

Hi One208.

Applescript:

-- 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


NG

Offline

 

#4 2021-09-16 04:10:06 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Location reference

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:

Applescript:


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

Last edited by KniazidisR (2021-09-16 07:42:10 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-09-16 05:06:16 am

One208
Member
Registered: 2021-03-15
Posts: 32

Re: Location reference

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

error "Can’t make «class docf» \"MacScripter / Location reference.scpt\" of «class cfol» \"Desktop\" of «class cfol» \"one\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\" into the expected type." number -1700 from «class docf» "MacScripter / Location reference.scpt" of «class cfol» "Desktop" of «class cfol» "one" of «class cfol» "Users" of «class sdsk»

Offline

 

#6 2021-09-16 06:21:30 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5427

Re: Location reference

One208 wrote:

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

error "Can’t make «class docf» \"MacScripter / Location reference.scpt\" of «class cfol» \"Desktop\" of «class cfol» \"one\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\" into the expected type." number -1700 from «class docf» "MacScripter / Location reference.scpt" of «class cfol» "Desktop" of «class cfol» "one" of «class cfol» "Users" of «class sdsk»


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.

Applescript:

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


NG

Offline

 

#7 2021-09-16 07:17:39 am

One208
Member
Registered: 2021-03-15
Posts: 32

Re: Location reference

Hi Nigel, This works very well

Thank you

Offline

 

#8 2021-09-16 07:46:15 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Location reference

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

Last edited by KniazidisR (2021-09-16 07:46:53 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#9 2021-09-16 08:59:50 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5427

Re: Location reference

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:

Applescript:

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


NG

Offline

 

#10 2021-09-16 09:57:50 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 898

Re: Location reference

@Nigel
You do not need to declare POSIX path to text, its the default behaviour.

If you ask for the class with or without declare it as text, it will return text


if you are the expert, who will you call if its not your imagination.

Online

 

#11 2021-09-16 10:32:21 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5427

Re: Location reference

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.


NG

Offline

 

#12 2021-09-16 11:11:24 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 898

Re: Location reference

@Nigel

When I run the code below in Mojave.
It create .scpt document on desktop and open it in Script Editor with the title of Safari
name of tab 1.

Here is your code modified:

Applescript:

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

-- I use Safari but it do the same as chrome.
tell application "Safari" to set ChrTitle to name of tab 1 of front window
set scriptName to ChrTitle & ".scpt"
set saveName to replaceText(scriptName, "/", "-")
set savePath to (path to desktop as text) & saveName -- HFS path.
-- Here I modified your code
set eventualPath to POSIX path of (path to desktop) & 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


if you are the expert, who will you call if its not your imagination.

Online

 

#13 2021-09-16 02:31:49 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5427

Re: Location reference

Fredrik71 wrote:

When I run the code below in Mojave.
It create .scpt document on desktop and open it in Script Editor with the title of Safari
name of tab 1.


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.


NG

Offline

 

#14 2021-09-16 03:52:35 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 898

Re: Location reference

@Nigel
The test I did:
The name of tab 1 of Safari (didn't include /) so it did work for me.

Maybe I didn't understood, sorry.


if you are the expert, who will you call if its not your imagination.

Online

 

#15 2021-09-16 04:23:31 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5427

Re: Location reference

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!  hmm


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)