Same system, same code, diff. Macs, diff. results

Hi,

I have built a stack, that gathers a list of ‘.scpt’ and ‘.applescript’ file paths from hard disk, then creates a new card for each file and finally pastes the files’ script into the card’s devoted field.

This succeeds with Mac Book Pro 17’’ (Mid 2009), but fails with Mac Pro (Mid 2010). On both machines work Mac OS X 10.11.5 (El Capitan), LiveCode 8.0.0 Build 13018, AppleScript 2.5, ScriptEditor 2.8.1 (183.1)

LiveCode part:
[format].
repeat with ii = 1 to the Number of lines in AS_FileList
put line ii of AS_FileList into FileLine
create card
put “set AS_FilePath to” && q(revMacFromUnixPath(FileLine)) into line 2 of AppleScript_Code
do ASS as AppleScript
select before fld “AS_Archivist_Script”
paste
end repeat
.[/format]

AppleScript_Code


tell application "Script Editor"
set AS_FilePath to "HardDisk/Folder 1/Folder 2/pdAS_FileLister¢jpg.scpt"
   set myScript to open AS_FilePath
   activate
   tell application "System Events"
      tell process "Script Editor"
         click menu item "Alles auswählen" of menu "Bearbeiten" of menu bar 1 -- "Select all" of "Edit"
         click menu item "Kopieren" of menu "Bearbeiten" of menu bar 1        -- "Copy"       of "Edit"
      end tell
   end tell
   close myScript
end tell

Any hint is welcome.

Peter

I can’t test the LiveCode part.

I tested a slightly different version.

As I had to build the pathname to the script to open, I move the instruction out of the tell application “Script Editor” block.
As I dislike scripts linked to a specific location I replaced two boring instructions by two ones which are valid worldwide.
I applied a tip introduced with Yosemite. Theorically it’s unneeded with El Capitan but I play safe. After all, the infamous bug requiring may come back one day.

Here is the code.

--set AS_FilePath to "HardDisk/Folder 1/Folder 2/pdAS_FileLister¢jpg.scpt"

set AS_FilePath to (path to desktop as text) & "créee un lien symbolique.scpt"
tell application "Script Editor"
	
	set myScript to open AS_FilePath
	activate
	tell application "System Events"
		tell process "Script Editor"
			set frontmost to true
			keystroke "a" using {command down} # this way the command is valid worldwide
			--click menu item "Alles auswählen" of menu "Bearbeiten" of menu bar 1 -- "Select all" of "Edit"
			keystroke "c" using {command down} # this way the command is valid worldwide
			--click menu item "Kopieren" of menu "Bearbeiten" of menu bar 1 -- "Copy"       of "Edit"
		end tell
	end tell
	close myScript
end tell

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) samedi 9 juillet 2016 19:37:27

Bon jour, Yvan,

thank you for your convincing tips,
a) to use “worldwide valid” keystrokes,
b) to reposition the pathname.

On the MacPro I had to delay the LiveCode process after “paste” a bit . Now I have a searchable AppleScript Archive.

Salut cordial

Peter

If you only want the source as text, there’s no need to use GUI scripting:

set AS_FilePath to "/Folder 1/Folder 2/pdAS_FileLister¢jpg.scpt"
set theFile to AS_FilePath as alias
-- get the source
tell application "Script Editor"
	open theFile
	tell document 1
		set theContents to contents
		close saving no
	end tell
end tell
set the clipboard to theContents

In fact, you can skip Script Editor:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set AS_FilePath to "/Folder 1/Folder 2/pdAS_FileLister¢jpg.scpt"
set theURL to current application's |NSURL|'s fileURLWithPath:AS_FilePath
set theScript to current application's NSAppleScript's alloc()'s initWithContentsOfURL:theURL |error|:(missing value)
set theSource to theScript's source() as text
set the clipboard to theSource

And if you want the source styled:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set AS_FilePath to "/Folder 1/Folder 2/pdAS_FileLister¢jpg.scpt"
set theURL to current application's |NSURL|'s fileURLWithPath:AS_FilePath
set theScript to current application's NSAppleScript's alloc()'s initWithContentsOfURL:theURL |error|:(missing value)
-- recompile and extract styled source
theScript's compileAndReturnError:(missing value)
set theSource to theScript's richTextSource()
-- put styled source on clipboard
set thePasteboard to current application's NSPasteboard's generalPasteboard()
-- clear it, then write new contents
thePasteboard's clearContents()
thePasteboard's writeObjects:{theSource}