New AppleScript projects

So that we can have a true apples-to-apples comparison, could someone post a link to a test file that we all could use to test against?

I am very curious how well the JXA JavaScript RegEx engine will perform against the same test file. So far I have found it to both very fast, and very easy to use.

Weā€™re not really comparing regex engines, though ā€“ itā€™s a question of how fast it can be done in a call from an AppleScript. I suspect the overhead of calling JXA from AppleScript will swamp the time actually taken to do the search and replace.

Interesting perspective.

I would NOT call JXA from AppleScript.
I would use JXA directly.

For a fair, complete test, it would need to include reading the file, making the changes, and writing to a new file.

Well the topic is ā€œNew AppleScript projectsā€, so it seems a reasonable perspective :slight_smile:

If JXA is your preference, and thereā€™s no AppleScript involved, Iā€™m not sure why youā€™d use anything else.

But if you want to post code equivalent to mine, Iā€™ll be glad to test it on the same file and Mac. However, I donā€™t expect much difference simply because I suspect the read/write is what takes more of the time. (And it would need timing code.)

You guys are great! Thanks so much for all the responses. Iā€™ll be pounding away at these for the next few weeks (something to keep my mind off the hand basket weā€™re all heading to hell in these days).

As I develop each project Iā€™ll post the scripts and sample files here, or in a public place.

Ed

Hi Shane,

Okay, thatā€™s impressive! You win that challenge.

My test machine is a bit slower: Mac Mini Server (Late 2012), 2.3 GHz Core i7. If run on your iMac, Iā€™d project my ā€˜sedā€™ script would take about 2.8 seconds, but thatā€™s still glacial compared to your 0.28 seconds.

My testing led to an observation about SD that surprised me. I ran my script several times with debug mode on and several times with it off. Debug mode execution times ranged 3.92-3.95 seconds, while non-debug mode ranged 4.01 to 4.04 seconds. Itā€™s not much of a difference, but Iā€™d expect debug mode to be slower, not faster. Whatā€™s going on?

Regards,
Stan C.

Script Debugger does try to take the debugging overhead into account. Unfortunately there are complications that mean any values are more indicative than totally accurate. In the real world, timings can differ considerably depending on the host.

Yes, that makes sense, Shane. Within a particular environment, SDā€™s timer can show relative speed differences. But itā€™s a bit like capturing a timestamp to the millisecondā€”you will get a number, but donā€™t expect a high degree of precision.

Stan C.

Thatā€™s right. I did some further timings, rewriting the code directly in Objective-C. Even putting the search in a loop to perform it 100 times, the results varied between runs by more than 10%. Putting in a search string that isnā€™t found in the text gives much more consistent times.

Anyway, my conclusion is that calling just the search via AS rather than directly in Objective-C adds maybe a few milliseconds per iteration. That doesnā€™t strike me as much overhead in the scheme of things.

Is there a recommended file extension for writing lists to files? It just dawned on me that Iā€™m not using any extension. I know they are text files, but they canā€™t be edited as text files.

Is there a best practice for this?

Would it be possible to set it up so theyā€™ll open in my applet (or in SD as a list) when double clicked?

No, theyā€™re not text files ā€“ theyā€™re a kind of binary file. Thereā€™s no prescribed extension, but it might be helpful to use something like .data.

I have a syncing question. I had planned to compare script modification dates and if the script on the server was changed after the script on the client mac the server script would replace the client script.

The problem is that with AppleScript any time you run a script that has globals or properties (and most of mine do) that can change the modification date. So itā€™s very possible that the user could end up missing updates.

Any suggestions?

Use version numbers. If theyā€™re not applets, use .scptd files.

Shaneā€™s suggestion is a very good but if for some reason if that causes a problem I have sometimes used the spotlight comments field. It can be easily set with a script and can be read from a script and from finder. But unless Shaneā€™s suggestion causes some kind of a problem Iā€™d use his suggestion. It is reliable and pretty tamper proof by someone in Finder. Iā€™ve never seen any other type of version control used in AppleScript except the version is put in the name, using the comments field or version numbers.

Bill

Also works with just plain .scpt files:

property version : "1.0"

Jim,

How does this work? I tried the script:

set ThePath to "Bills second iMac HD:Users:bill:Desktop:untitled folder 30:Untitled.scpt"
tell application "Finder"
	set version of (item ThePath) to 1.0
end tell
```
I just got the error, "Finder got an error: Canā€™t set version of item "Bills second iMac HD:Users:bill:Desktop:untitled folder 30:Untitled.scpt" to 1.0."  I'm curious what did you use for to script to change it.

However using the version control built into script debugger seems pretty convent.

Bill

Thatā€™s true, but they then donā€™t show up in Get Info windows. The only access is to load the scripts.

Mind you, the Finder and System Events seem brain dead scripting the property with bundles too.

Well nothing is ever easy, is it?

I downloaded a third party syncing app, but it has the same issue with modification dates changing due to AppleScriptā€™s saving when run.

I was going to try changing the creation date when I update files, but you canā€™t do that in Finder or System Events. Is there a shell that does that? (Iā€™m also going to look at the rsync shell)

Meantime Iā€™m going to try two options: Version; and if thatā€™s not fast reliable Iā€™ll build data files that track the changes made to the source scripts on the server and the local scripts locally. (The advantage of that is that the client mac can simply compare the network file to the local file to know if there are any updates and which scripts need updates.) The file will simply indicate the last time any specific file was updated, and if the last update on the server is newer than the client it will change it.

For apps, getting the version is dead simple:

set myVersion to version of application (path to me as text)
set masterPath to "Macintosh HD:Users:shane:Desktop:With version bundle app.app"
set currentVersion to version of application masterPath
considering numeric strings
	if myVersion < currentVersion then
		-- 
	end if
end considering

For script bundles, you do exactly the same ā€“ just pretend theyā€™re apps.

Yes you can change both modification and creation dates with a shell script. I through together a quicky script for this. Itā€™s no eloquent but it shows how to do it. Itā€™s a single SetFile command in terminal so the shell script is very simple.

Well hereā€™s the script:

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

on SecondsToTime(TheSeconds, DesiredDecimalPlaces)
	if (TheSeconds > 86400) then -- 86400 seconds in a day
		set TheTime to MakeFixedDecimalPlaces((TheSeconds / 86400), 2) & " days"
	else if (TheSeconds > 3600) then -- 3600 seconds in an hour
		set TheTime to MakeFixedDecimalPlaces((TheSeconds / 3600), 2) & " hours"
	else if (TheSeconds > 60) then -- 60 seconds in an hour
		set TheTime to MakeFixedDecimalPlaces((TheSeconds / 60), 2) & " minutes"
	else
		set TheTime to MakeFixedDecimalPlaces(TheSeconds, 2) & " seconds"
	end if
	
	return TheTime
end SecondsToTime

on MakeFixedDecimalPlaces(TheNum, DesiredDecimalPlaces)
	local TheNumStr
	local ReverseStr
	local TheOffest
	local PadStr
	local ZeroString
	
	try
		if (DesiredDecimalPlaces = 0) then
			-- Just return integer part of number
			return (TheNum div 1) as string
		else if (TheNum < 0) then
			display dialog "TheNum in MakeFixedDecimalPlaces was a negative number." buttons {"OK"} default button "OK" giving up after 3 with title "Error"
			-- Signal error
			return "?"
		else if (DesiredDecimalPlaces < 0) then
			display dialog "DesiredDecimalPlaces was a negative number." buttons {"OK"} default button "OK" giving up after 3 with title "Error"
			-- Signal error
			return "?"
		end if
		
		set TheNumStr to TheNum as string
		
		if ((offset of "." in TheNumStr) = 0) then
			set ZeroString to strings 1 thru DesiredDecimalPlaces of "000000000000000"
			return TheNumStr & "." & ZeroString
		end if
		
		set ReverseStr to ((reverse of characters of TheNumStr) as string)
		
		-- Find the offset of the period character
		set TheOffest to offset of "." in ReverseStr -- Only returns positive numbers or zero
		if (TheOffest = 0) then
			set ActualDecimalPlaces to 0
		else
			-- Subtract 1 because when no descimal places then have to suctract the space the "." usualy takes up
			set ActualDecimalPlaces to TheOffest - 1
		end if
		
		if (ActualDecimalPlaces = DesiredDecimalPlaces) then
			return TheNumStr
		else if (ActualDecimalPlaces < DesiredDecimalPlaces) then
			-- Pad end of TheNumStr with "0"
			set NumOfZerosToAppend to DesiredDecimalPlaces - ActualDecimalPlaces
			set ZeroString to strings 1 thru NumOfZerosToAppend of "000000000000000"
			return TheNumStr & ZeroString
		else
			-- ActualDecimalPlaces > DesiredDecimalPlaces
			-- Cut characters off end of string
			set NumOfCharsToCutOff to ActualDecimalPlaces - DesiredDecimalPlaces
			return strings 1 thru (-1 * NumOfCharsToCutOff - 1) of TheNumStr
		end if
	on error errMsg number errNum
		display dialog "Error " & (errNum as string) & " occured." & return & return & errMsg Ā¬
			with title "Error"
		-- Signal error
		return "?"
	end try
end MakeFixedDecimalPlaces

on TwoDigitNum(TheNum)
	if (length of (TheNum as string) = 1) then
		set TheNum to " " & TheNum
	end if
	return TheNum
end TwoDigitNum

on FormatedDateString(TheDate)
	set DateString to "'" & TwoDigitNum(((month of TheDate as number) as string)) & "/"
	set DateString to DateString & TwoDigitNum(((day of TheDate as number) as string)) & "/"
	set DateString to DateString & TwoDigitNum(((year of TheDate as number) as string)) & " "
	
	set DateString to DateString & time string of TheDate & "'"
	
	return DateString
end FormatedDateString

tell application "Finder"
	set TheSelection to selection
	set NumberSelected to count TheSelection
	if (NumberSelected ā‰  1) then
		display dialog "Exactly 1 file must be selected in Finder." buttons {"Cancel", "OK"} default button "OK" with title "error"
		return false
	end if
	set TheItem to item 1 of TheSelection
	if ((class of TheItem) = folder) then
		display dialog "A Folder can not be selected in Finder." buttons {"Cancel", "OK"} default button "OK" with title "error"
		return false
	end if
	set PosixFilePath to POSIX path of (TheItem as string)
end tell

set CreationDateStr to FormatedDateString(current date)
set ModDateStr to FormatedDateString(current date)

do shell script "SetFile -d " & CreationDateStr & " \"" & PosixFilePath & "\"" --change the creation date of the file to the current date
do shell script "SetFile -m " & ModDateStr & " \"" & PosixFilePath & "\"" --change the modification date of the file to the current date

Sorry, I didnā€™t have time to clean it up before sending but I have to get back to what I was doing. You can change modification or creation date or both.

Bill