Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Shared Applescript App Quits Unexpectedly

Hello! First off - Thank you to anyone who attempts to give me some help, anything is greatly appreciated. I'm a little stumped. I've written as much attached info to try and describe my problem as possible, but essentailly I'm having trouble sharing a droplet with other mac users.


My computer is:

iMac (21.5-inch, Late 2013)

2.7 GHz Intel Core i5

8 GB 1600 MHz DDR3

Iris Pro Graphics

OS X 10.10.5


I am writing applscript code with

Script Editor Version 2.7 (176)

AppleScript 2.4


App Overview:

The app I've written works on my computer 100% of the time - This makes me think that the code isn't the problem, but I will post it at the bottom regardless. I am not sure what everything I've written is essential for, and a lot of this code was tested through trial and error.

Essentially it takes a dropped folder as input. Asks the user for 2 different inputs of text. It then takes that information, renames the folder, as well as renames all image files inside the folder with sequentially increasing suffixes. Then also changes the names of two different types of files slightly differently.


Problem Overview:

What I'm having a problem with is when I try to send my droplet application to other mac users and having them run it successfully. Originally I had created this same app with the Automater, however it wouldn't run on any other computer (my best guess was operating system incompatibility). So I went and learned as much as I could about applescript, and tried to write the code to be as back compatible as possible. Now when another user attempts to use the droplet it says it "quit unexpectedly". The method to give the app to another user: I zipped the app, then emailed it. In emailing it to myself, opening it and running it, there were no issues here.


I'm not sure if the problem I have is based in the code I've written, or somewhere else along the supply chain.


Error Log from my beta tester (i'm not exactly sure what I am looking at here, but it might be helpful to someone here. I've removed anything that looked to me like it could be sensitive. If it's important, let me know and I can share it)

______________________________________________________________________________

Error Log:

______________________________________________________________________________


Process: launchd [7706]

Path: /Users/username/Desktop/appname.app/Contents/MacOS/droplet

Identifier: com.apple.ScriptEditor.id.Renaming-Copy

Version: ??? (???)

Code Type: X86-64 (Native)

Parent Process: launchd [98]


Date/Time: 2015-08-28 16:23:09.821 -0400

OS Version: Mac OS X 10.6.8 (10K549)

Report Version: 6


Interval Since Last Report: 1241996 sec

Crashes Since Last Report: 22178

Per-App Crashes Since Last Report: 24

Anonymous UUID: <removed for security>


Exception Type: EXC_BAD_ACCESS (SIGSEGV)

Exception Codes: KERN_INVALID_ADDRESS at 0xfffffffffffffff8

Crashed Thread: Unknown


Backtrace not available


Unknown thread crashed with X86 Thread State (64-bit):

rax: 0x0000000000000055 rbx: 0x0000000000000000 rcx: 0x0000000000000000 rdx: 0x0000000000000000

rdi: 0x0000000000000000 rsi: 0x0000000000000000 rbp: 0x0000000000000000 rsp: 0x0000000000000000

r8: 0x0000000000000000 r9: 0x0000000000000000 r10: 0x0000000000000000 r11: 0x0000000000000000

r12: 0x0000000000000000 r13: 0x0000000000000000 r14: 0x0000000000000000 r15: 0x0000000000000000

rip: 0x00007fff5fc01028 rfl: 0x0000000000010203 cr2: 0xfffffffffffffff8


Binary images description not available



Model: MacBookAir3,1, BootROM MBA31.0061.B01, 2 processors, Intel Core 2 Duo, 1.4 GHz, 4 GB, SMC 1.67f10

Graphics: NVIDIA GeForce 320M, NVIDIA GeForce 320M, PCI, 256 MB

Memory Module: global_name

AirPort: spairport_wireless_card_type_airport_extreme (0x14E4, 0xD1), Broadcom BCM43xx 1.0 (<removed for security>)

Bluetooth: Version 2.4.5f3, 2 service, 19 devices, 1 incoming serial ports

Network Service: AirPort, AirPort, en0

Serial ATA Device: APPLE SSD TS128C, 113 GB

USB Device: FaceTime Camera (Built-in), 0x05ac (Apple Inc.), 0x850a, 0x24600000 / 2

USB Device: BRCM2070 Hub, 0x0a5c (Broadcom Corp.), 0x4500, 0x04500000 / 3

USB Device: Bluetooth USB Host Controller, 0x05ac (Apple Inc.), 0x821b, 0x04530000 / 5

USB Device: Apple Internal Keyboard / Trackpad, 0x05ac (Apple Inc.), 0x0242, 0x04300000 / 2

______________________________________________________________________________

end of error log

______________________________________________________________________________

______________________________________________________________________________

Apple Script: Appologies - I can't seem to format this as an applescript in here. If there is any easier way to display this for you please comment and let me know how and I'll do my best to edit this so it's easier to help me out!

This script is mostly a combination of my own mind and a previously written script here:

https://gist.github.com/oliveratgithub/b9030365c9ae483984ea

______________________________________________________________________________


-- This takes the dropped file and saves it as alias

on open the_dropped_folder as alias

-- saving the text item delimiters

set olddelimiter to text item delimiters

-- this is used to break apart the file name and the extension later

set text item delimiters to "."

tell application "Finder"

-- these are incased in try blocks to fail silently if not all kind's of files are contained in the folder

-- they group like items together by the kind of file they are, for later renaming.

try

get every item of the_dropped_folder whose kind contains "Image"

set the_jpg_files to the result

end try

try

get every item of the_dropped_folder whose kind contains "PDF"

set the_pdf_files to the result as list

set pdf_file to the first item of the_pdf_files

end try

try

get every item of the_dropped_folder whose kind contains "Unix"

set the_ax5_files to the result as list

set ax5_file to the first item of the_ax5_files

end try

-- these two dialogs are the user input that allows the application to rename the files based on the desired output.

display dialog "What is the Folder Name?

(Ex: John Smith 123 Grove Street would be jsh123)" default answer "abc123"

set folder_name to text returned of result

display dialog "What is the Survey Number?

(Usually a 7-8 digit number beginning with 100)" default answer "100xxxx"

set survey_number to text returned of result

-- this is converting the alias files to a list

set all_files to every item of the_jpg_files as list

-- this enters a repeat block, renaming each individual file and adding a suffix before the extension that increases sequentially according to it's place in the index.

repeat with index from 1 to the count of all_files

--using our index, we select the appropriate file from our list

set this_file to item index of all_files

set file_name_count to text items of (get name of this_file)

--if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later

if index is less than 10 then

set index_prefix to "0"

else

set index_prefix to ""

end if

--lets check if the current file from our list (based on index-number) has even any file-extension

if number of file_name_count is 1 then

--file_name-count = 1 means, we extracted only 1 text-string from the full file name. So there is no file-extension present.

set file_extension to ""

else

--yup, we are currently processing a file that has a file-extension

--we have to re-add the original file-extension after changing the name of the file!

set file_extension to "." & item -1 of file_name_count

end if

--let's rename our file, add the sequential number from 'index' and add the file-extension to it

set the name of this_file to folder_name & " " & index_prefix & index & file_extension as string

end repeat

-- here we attempt to rename a PDF file and an ax5 file if they are contained in the folder at all.

try

set the name of pdf_file to folder_name & " " & survey_number & ".pdf" as string

end try

try

set the name of ax5_file to folder_name & " " & survey_number & ".ax5" as string

end try

-- here the user gets a report on the number of files renamed, as well as what types were processed. This is a bit of a cheat though, because had there been no PDF or ax5 files, the dialog would not change and you would be told falsly that 1 pdf and 1 ax5 file were renamed. This wasn't a big deal to me as the folder's will always be organized the same, the important part is the image renaming.

display dialog "Renamed: " & index + 2 & " Files" & "

" & index & " .jpg files with " & folder_name & " " & "

" & "1 PDF File with " & folder_name & " " & survey_number & "

" & "1 AX5 File with " & folder_name & " " & survey_number

set the name of the_dropped_folder to folder_name & " WIP"

-- returning the text item delimiters

set text item delimiters to olddelimiter

end tell

end open


______________________________________________________________________________

End of Apple Script

______________________________________________________________________________


Thanks again for any help you might be able to offer. Happy to answer any questions to get to the bottom of this problem. Also happy to post this into a different location if this is the wrong one!

iMac (21.5-inch, Late 2013), OS X Yosemite (10.10.5)

Posted on Aug 28, 2015 2:31 PM

Reply
Question marked as Best reply

Posted on Aug 28, 2015 3:29 PM

Hello


Try replacing this line:


on open the_dropped_folder as alias



with these two lines


on open the_dropped_folder set the_dropped_folder to the_dropped_folder as alias




Handler parameter with type declaration is introduced in AppleScript under OS X 10.10 and is apparently not backward compatible.


You'll have to peruse the release notes if you want to write backward compatible scripts, for they keep changing this legacy language recently.


https://developer.apple.com/library/mac/releasenotes/AppleScript/RN-AppleScript/



Hope this may help,

H

16 replies
Question marked as Best reply

Aug 28, 2015 3:29 PM in response to Jeeekel

Hello


Try replacing this line:


on open the_dropped_folder as alias



with these two lines


on open the_dropped_folder set the_dropped_folder to the_dropped_folder as alias




Handler parameter with type declaration is introduced in AppleScript under OS X 10.10 and is apparently not backward compatible.


You'll have to peruse the release notes if you want to write backward compatible scripts, for they keep changing this legacy language recently.


https://developer.apple.com/library/mac/releasenotes/AppleScript/RN-AppleScript/



Hope this may help,

H

Aug 28, 2015 11:01 PM in response to Hiroto

I'll have to check tomorrow if this is all the solution requires. It looks like if not this particular part of the code, then I've got some research to do to eliminate this as the issue.


Thank you very much! Very helpful. I'll reply again once I know if this particular part made it work or if there were other parts as well.

Aug 28, 2015 11:32 PM in response to Jeeekel

Another thing to look at is that the open handler is passed a list (even if there is only one item), so if more than one item is dropped the coercion to alias will fail, unless you just get the first item or whatever. You should probably also check if the dropped items actually contain folders, since it looks like that is what you are expecting - never underestimate the ability of other users to find creative ways to crash your application.

Aug 29, 2015 9:49 AM in response to Jeeekel

Running production apps brings a whole new set of problems. You may want to consider adding logging to your app. Here is an example app with a built in logging to a file.


(* 
   It is easier to diagnose problems with debug information. This applescript demonstaties how to create a log file.

    Author: rccharles
    
    Write debug text to the file debugLog.txt in your home folder ( ~/debugLog.txt ).
    
    example of the file ~/debugLog.txt:
    
New log created on Wednesday, May 15, 2013 2:12:41 PM 
   --- debug on Wednesday, May 15, 2013 2:12:41 PM   --- 
   --- current AppleScript is Ext-Remainder:Applications:applescriptFiles:demo:log user data.app: ---
start program.  
thePath = Ext-Remainder:
End of program.  
   --- debug on Wednesday, May 15, 2013 2:15:14 PM   --- 
   --- current AppleScript is Ext-Remainder:Applications:applescriptFiles:demo:log user data.app: ---
start program.  
thePath = Ext-Remainder:
End of program.  

    
    fyi:
    
    The applescript log statements are ignored when not running from the script editor.
    
    Not to be confused, this applescript contains log statements that have nothing to do with creating a log file. 
    They are to help the script auther write the script.
    
 *)



on run
    -- thePath points to the folder in which to create the debug log.
    global thePath, firstRunning
    (* Use the path to clause to create a generalized  path statements *)
    set thePath to (path to home folder as string)
    set firstRunning to ""
    
    -- Write a message into the applescript editor event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    debug("start program.  ")
    
    (* 
    
    Your program here...
    
    *)
    debug("thePath = " & thePath)
    
    debug("End of program.  ")
    
    
    
end run

-- ----------------------------------------------------------

on appendToFile(fileId, theData)
    
    local theSize, writeWhere
    
    set theSize to (get eof fileId)
    set writeWhere to theSize + 1 as integer
    write theData to fileId starting at writeWhere
    
end appendToFile


-- ----------------------------------------------------------

-- based on log by James Reynolds, 12.18.2000, University of Utah

on debug(theMessage)
    global thePath, firstRunning
    local theSize, startupDiskName, pathToLog, fileReference, appPath
    
    set pathToLog to (thePath & "debugLog.txt")
    try
        
        set fileReference to (open for access file pathToLog ¬
            with write permission)
        log "firstRunning = " & firstRunning
        set theSize to (get eof fileReference)
        if firstRunning = "" then
            set theSize to (get eof fileReference)
            log "theSize = " & theSize
            if theSize is equal to 0 then
                appendToFile(fileReference, "New log created on " & ((current date) as string) & " " & return)
                tell application "Finder"
                    set the creator type of the file pathToLog ¬
                        to "R*ch"
                end tell
            end if
            appendToFile(fileReference, "   --- debug on " & ((current date) as string) & "   --- " & return)
            tell current application
                set appPath to path to current application
            end tell
            appendToFile(fileReference, "   --- current AppleScript is " & appPath & " ---" & return)
            
            set firstRunning to "running"
        end if
        
        
        appendToFile(fileReference, theMessage & return)
        
        close access fileReference
        
    on error mes
        try
            log "  We got an error when writing to  " & mes
            close access fileReference
        end try
    end try
    
end debug

Aug 29, 2015 9:51 AM in response to red_menace

How would you suggest getting around this?

if the count of the_dropped_folder is > 1 then

display dialog "Please select one folder at a time" when using this program.

end if


This part is a bit foreign to me. I'm not sure how to check if a file is a folder, whenever I try to check if an item's kind is folder it seems to fail. Maybe i'm just writing the statement wrong?

if the_dropped_folder contains kind folder then

display dialog "Please ensure only images, pdf's, and ax5 files are present in the target folder"

end if

Aug 29, 2015 10:23 AM in response to rccharles

Oh this seems really smart. I think it might be a bit above my head at this stage of my knowledge though. I'm not sure how to get it to work. The way my script is written is set as a droplet, so I changed that to target a specific test folder. However now i'm getting an error that says "The run handler is specified more than once, or there were top-level commands in addition to the run handler." I'm not exactly sure what the other top-level command would be.


I think I've got a lot of research to do. If you get around to reading this, i've included the combined scripts, there's something proabbly very obvious I'm missing here, but I'm not sure what it is. I'm researching handlers right now to try and figure this out right now.


Thanks for your time!!


on run
    -- thePath points to the folder in which to create the debug log. 
    global thePath, firstRunning
    (* Use the path to clause to create a generalized  path statements *)
    set thePath to (path to desktop folder as string)
    set firstRunning to ""
   
    -- Write a message into the applescript editor event log. 
    log "  --- Starting on " & ((current date) as string) & " --- "
   
    debug("start program.  ")
   
-- This takes the dropped file and saves it as alias
-- this was changed in an attempt to debug the script
tell application "Finder"
set the_dropped_folder to folder "abc123 WIP" of (path to desktop) as alias
    set the_dropped_folder to the_dropped_folder as alias
    -- saving the text item delimiters
    set olddelimiter to text item delimiters
    -- this is used to break apart the file name and the extension later
    set text item delimiters to "."
    tell application "Finder"
        -- these are incased in try blocks to fail silently if not all kind's of files are contained in the folder
        -- they group like items together by the kind of file they are, for later renaming.
        try
            get every item of the_dropped_folder whose kind contains "Image"
            set the_jpg_files to the result as list
        end try
       
        try
            get every item of the_dropped_folder whose kind contains "PDF"
            set the_pdf_files to the result as list
            set pdf_file to the first item of the_pdf_files
        end try
       
        try
            get every item of the_dropped_folder whose kind contains "Unix"
            set the_ax5_files to the result as list
            set ax5_file to the first item of the_ax5_files
        end try
        -- these two dialogs are the user input that allows the application to rename the files based on the desired output.
        display dialog "What is the Folder Name?
(Ex: John Smith 123 Grove Street would be jsh123)" default answer "abc123"
        set folder_name to text returned of result
       
        display dialog "What is the Survey Number?
(Usually a 7-8 digit number beginning with 100)" default answer "100xxxx"
        set survey_number to text returned of result
       
        -- this is converting the alias files to a list
        set all_files to every item of the_jpg_files as list
        -- this enters a repeat block, renaming each individual file and adding a suffix before the extension that increases sequentially according to it's place in the index.
        repeat with index from 1 to the count of all_files
            --using our index, we select the appropriate file from our list
            set this_file to item index of all_files
            set file_name_count to text items of (get name of this_file)
            --if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
            if index is less than 10 then
                set index_prefix to "0"
            else
                set index_prefix to ""
            end if
            --lets check if the current file from our list (based on index-number) has even any file-extension
            if number of file_name_count is 1 then
                --file_name-count = 1 means, we extracted only 1 text-string from the full file name. So there is no file-extension present.
                set file_extension to ""
            else
                --yup, we are currently processing a file that has a file-extension
                --we have to re-add the original file-extension after changing the name of the file!
                set file_extension to "." & item -1 of file_name_count
            end if
            --let's rename our file, add the sequential number from 'index' and add the file-extension to it
            set the name of this_file to folder_name & " " & index_prefix & index & file_extension as string
        end repeat
        -- here we attempt to rename a PDF file and an ax5 file if they are contained in the folder at all.
        try
            set the name of pdf_file to folder_name & " " & survey_number & ".pdf" as string
        end try
        try
            set the name of ax5_file to folder_name & " " & survey_number & ".ax5" as string
        end try
        -- here the user gets a report on the number of files renamed, as well as what types were processed. This is a bit of a cheat though, because had there been no PDF or ax5 files, the dialog would not change and you would be told falsly that 1 pdf and 1 ax5 file were renamed. This wasn't a big deal to me as the folder's will always be organized the same, the important part is the image renaming.
        display dialog "Renamed: " & index + 2 & " Files" & "
    " & index & " .jpg files with " & folder_name & " " & "
    " & "1 PDF File with " & folder_name & " " & survey_number & "
    " & "1 AX5 File with " & folder_name & " " & survey_number
        set the name of the_dropped_folder to folder_name & " WIP"
        -- returning the text item delimiters
        set text item delimiters to olddelimiter
    end tell
    end tell
    --end open removed here
end

    debug("thePath = " & thePath)
   
    debug("End of program.  ")
   
   
   
end run

-- ---------------------------------------------------------- 

on appendToFile(fileId, theData)
   
    local theSize, writeWhere
   
    set theSize to (get eof fileId)
    set writeWhere to theSize + 1 as integer
    write theData to fileId starting at writeWhere
   
end appendToFile


-- ---------------------------------------------------------- 

-- based on log by James Reynolds, 12.18.2000, University of Utah 

on debug(theMessage)
    global thePath, firstRunning
    local theSize, startupDiskName, pathToLog, fileReference, appPath
   
    set pathToLog to (thePath & "debugLog.txt")
    try
       
        set fileReference to (open for access file pathToLog ¬
            with write permission)
        log "firstRunning = " & firstRunning
        set theSize to (get eof fileReference)
        if firstRunning = "" then
            set theSize to (get eof fileReference)
            log "theSize = " & theSize
            if theSize is equal to 0 then
                appendToFile(fileReference, "New log created on " & ((current date) as string) & " " & return)
                tell application "Finder"
                    set the creator type of the file pathToLog ¬
                        to "R*ch"
                end tell
            end if
            appendToFile(fileReference, "  --- debug on " & ((current date) as string) & "  --- " & return)
            tell current application
                set appPath to path to current application
            end tell
            appendToFile(fileReference, "  --- current AppleScript is " & appPath & " ---" & return)
           
            set firstRunning to "running"
        end if
       
       
        appendToFile(fileReference, theMessage & return)
       
        close access fileReference
       
    on error mes
        try
            log "  We got an error when writing to  " & mes
            close access fileReference
        end try
    end try
   
end debug

Aug 29, 2015 2:30 PM in response to Jeeekel

Hello


The situation is worse than I thought. AppleScript applet shell created in OS X 10.10 appears not backward compatible with at least OS X 10.7 or earlier according to the following report.


Yosemite AppleScript applications crash on Lion

http://lists.apple.com/archives/applescript-users/2014/Oct/msg00191.html



So your options would be as follows.


a) Distribute the source code of the script and let user compile and save it as application in user's environment; or,


b) Compile and save the script as application using target OSes and distribute applet per OS version.



In case of b), you'd need at least two versions – one for OS X 10.10 and other for OS X 10.9 and earlier. For your convenience, you might create empty applet by removing Contents/Resources/Scripts/main.scpt from applet bundle created in each OS version and reuse it as template per version where you can put the common compiled script at Contents/Resources/Scripts/main.scpt.


Regards,

H

Aug 30, 2015 10:43 AM in response to Hiroto

So your options would be as follows.

While not the original poster, I'd following too!


Is the source code the same in both cases?


I gather it is the same. I'd proceed by compiling once on 10.10. Then compile again on an earlier version of OS X with different name. Distribution would be confusing. Users would get confused over what to do.


Aren't these applets distributed in source anyway? Little special to do but avoid distributing as read only.


R

Aug 30, 2015 4:40 PM in response to rccharles

This may be of interest for processing dropped items and for debugging. It lets you debug from the script editor.


(* 
  Demonstration of how dropping files on AppleScript icon works.  Shows how to debug via on run path. Shows items added to folder.
  
 Save as an Application Bundle.  Don't check anything.

 Shows log statement.

 It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
    
For testing, run in the Script Editor.
    1) Click on the Event Log tab to see the output from the log statement
    2) Click on Run


Author: rccharles

 *)


-- Gets invoked here when you run in AppleScript editor.

on run
    --  debug lines
    set desktopPath to (path to desktop) as string
    
    -- here is a log statment.
    log "desktopPath = " & desktopPath
    
    -- Be sure to select a file on your DESKTOP.
    set see to alias (desktopPath & "Picture 1.png")
    
    -- Simulate dropped items list.
    set dropped_items to {see}
    
    common(dropped_items)
    
end run



-- Gets invoked here when something is dropped on the folder
-- Folder actions.

on adding folder items to this_folder after receiving added_items
    
    common(added_items)
    
end adding folder items to



-- Gets invoked here when something is dropped on this AppleScript icon

on open dropped_items
    
    common(dropped_items)
    
end open



on common(dropped_items)
    
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    
    log "class = " & class of dropped_items
    
    
    repeat with droppedItem in dropped_items
        
        display dialog "here is something that we got as input: " & droppedItem giving up after 15
    end repeat
    
    
end common

Aug 30, 2015 8:24 PM in response to rccharles

This has been lightly tested. Didn't focus on your code. changes:

-- restructured high level routines. Has two entry paths. on run for debug in script editor. on drop for dropping folder

-- added debug log

-- added folder check

-- loop through all folders drop. ( not tested beyond one)



(* 
  Demonstration of how dropping files on AppleScript icon works.  Shows how to debug via on run path. Shows items added to folder.
  
 Save as an Application Bundle.  Don't check anything.

 Shows log statement.

 It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
    
For testing, run in the Script Editor.
    1) Click on the Event Log tab to see the output from the log statement
    2) Click on Run


Author: rccharles

 *)


-- Gets invoked here when you run in AppleScript editor.

on run
    
    set desktopPath to (path to desktop) as string
    log "desktopPath = " & desktopPath
    
    -- Be sure to select a folder on your DESKTOP.
    -- colon is path separator.  Colon appears after folder name too.
    -- !!! Note this folder will get renamed !!! 
    
    -- set desktopPath to desktopPath & "test.png"
    set desktopPath to desktopPath & "asctest" & ":"
    
    log "desktopPath = " & desktopPath
    
    -- Be sure to select a file on your DESKTOP.
    set see to desktopPath as alias
    -- Simulate dropped items list.
    set dropped_items to {see}
    
    common(dropped_items)
    
end run



-- Gets invoked here when something is dropped on this AppleScript icon

on open dropped_items
    
    common(dropped_items)
    
end open



on common(dropped_items)
    -- thePath points to the folder in which to create the debug log.  
    global thePath, firstRunning
    (* Use the path to clause to create a generalized  path statements *)
    set thePath to (path to home folder as string)
    set firstRunning to ""
    
    -- Write a message into the applescript editor event log.  
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    debug("                    <=== start program.  ===>")
    
    
    
    
    -- Write a message into the event log.
    log "  --- Starting on " & ((current date) as string) & " --- "
    
    log "class = " & class of dropped_items
    
    -- you always get a list whether you get one or more items.
    repeat with droppedItem in dropped_items
        try
            log "class droppedItem= " & class of droppedItem
            debug("dropped item is: " & droppedItem as string)
        end try
        -- verify that it is a folder.  ( could it be an app bundle?  )
        -- http://macscripter.net/viewtopic.php?id=26813
        
        if folderExists(droppedItem as string) then
            
            -- seems that it is already an alias.
            set the_dropped_folder to droppedItem as alias
            
            -- saving the text item delimiters
            
            set olddelimiter to text item delimiters
            
            -- this is used to break apart the file name and the extension later
            
            set text item delimiters to "."
            
            tell application "Finder"
                
                -- these are incased in try blocks to fail silently if not all kind's of files are contained in the folder
                
                -- they group like items together by the kind of file they are, for later renaming.
                
                try
                    
                    get (every item of the_dropped_folder whose kind contains "Image")
                    
                    set the_jpg_files to the result
                    
                end try
                
                
                
                try
                    
                    get (every item of the_dropped_folder whose kind contains "PDF")
                    
                    set the_pdf_files to the result as list
                    
                    set pdf_file to the first item of the_pdf_files
                    
                end try
                
                
                
                try
                    
                    get (every item of the_dropped_folder whose kind contains "Unix")
                    
                    set the_ax5_files to the result as list
                    
                    set ax5_file to the first item of the_ax5_files
                    
                end try
                
                -- these two dialogs are the user input that allows the application to rename the files based on the desired output.
                
                display dialog "What is the Folder Name?

(Ex: John Smith 123 Grove Street would be jsh123)" default answer "abc123"
                
                set folder_name to text returned of result
                
                
                
                display dialog "What is the Survey Number?

(Usually a 7-8 digit number beginning with 100)" default answer "100xxxx"
                
                set survey_number to text returned of result
                
                
                
                -- this is converting the alias files to a list
                
                set all_files to every item of the_jpg_files as list
                
                -- this enters a repeat block, renaming each individual file and adding a suffix before the extension that increases sequentially according to it's place in the index.
                
                repeat with index from 1 to the count of all_files
                    
                    --using our index, we select the appropriate file from our list
                    
                    set this_file to item index of all_files
                    
                    set file_name_count to text items of (get name of this_file)
                    
                    --if the index number is lower than 10, we will add a preceding "0" for a proper filename sorting later
                    
                    if index is less than 10 then
                        
                        set index_prefix to "0"
                        
                    else
                        
                        set index_prefix to ""
                        
                    end if
                    
                    --lets check if the current file from our list (based on index-number) has even any file-extension
                    
                    if number of file_name_count is 1 then
                        
                        --file_name-count = 1 means, we extracted only 1 text-string from the full file name. So there is no file-extension present.
                        
                        set file_extension to ""
                        
                    else
                        
                        --yup, we are currently processing a file that has a file-extension
                        
                        --we have to re-add the original file-extension after changing the name of the file!
                        
                        set file_extension to "." & item -1 of file_name_count
                        
                    end if
                    
                    --let's rename our file, add the sequential number from 'index' and add the file-extension to it
                    set theMsg to " error # 1"
                    
                    try
                        -- I'm not always sure what can be made a string.  
                        -- ensure the original code can be run. 
                        set theMsg to "renamed " & (pdf_file as string) & " to " & folder_name & " " & index_prefix & index & file_extension as string
                    end try
                    log "theMsg= " & theMsg
                    set the name of this_file to folder_name & " " & index_prefix & index & file_extension as string
                    
                    -- not certain why the my is needed, but my forces this to be a routine in this file or something.
                    -- fix for error 1708
                    my debug(theMsg)
                    
                    
                end repeat
                
                -- here we attempt to rename a PDF file and an ax5 file if they are contained in the folder at all.
                
                try
                    try
                        set theMsg to " error # 2"
                        set theMsg to "renamed " & (pdf_file as string) & " to " & folder_name & " " & survey_number & ".pdf" as string
                    end try
                    
                    set the name of pdf_file to folder_name & " " & survey_number & ".pdf" as string
                    my debug(theMsg)
                end try
                
                try
                    try
                        set theMsg to " error # 3"
                        set theMsg to "renamed " & (ax5_file as string) & " to " & folder_name & " " & survey_number & ".ax5" as string
                    end try
                    
                    set the name of ax5_file to folder_name & " " & survey_number & ".ax5" as string
                    my debug(theMsg)
                end try
                
                -- here the user gets a report on the number of files renamed, as well as what types were processed. This is a bit of a cheat though, because had there been no PDF or ax5 files, the dialog would not change and you would be told falsly that 1 pdf and 1 ax5 file were renamed. This wasn't a big deal to me as the folder's will always be organized the same, the important part is the image renaming.
                
                display dialog "Renamed: " & index + 2 & " Files" & "

    " & index & " .jpg files with " & folder_name & " " & "

    " & "1 PDF File with " & folder_name & " " & survey_number & "

    " & "1 AX5 File with " & folder_name & " " & survey_number
                try
                    set the name of the_dropped_folder to folder_name & " WIP"
                on error errStr number errorNumber
                    display dialog "Oh no... could not rename folder! Did you run with the same folder twice?  Error info: " & errStr
                end try
                
            end tell
            
            
            -- returning the text item delimiters
            
            set text item delimiters to olddelimiter
            
        else
            -- user dropped something otherthan a folder :-( 
            display dialog "You need to drop a folder!"
            
        end if
        
    end repeat
    
    debug("End of program.  ")
    
end common



-- ----------------------------------------------------------  

on appendToFile(fileId, theData)
    
    local theSize, writeWhere
    
    set theSize to (get eof fileId)
    set writeWhere to theSize + 1 as integer
    write theData to fileId starting at writeWhere
    
end appendToFile


-- ----------------------------------------------------------  

-- based on log by James Reynolds, 12.18.2000, University of Utah  

on debug(theMessage)
    global thePath, firstRunning
    local theSize, startupDiskName, pathToLog, fileReference, appPath
    
    set pathToLog to (thePath & "debugLog.txt")
    try
        
        set fileReference to (open for access file pathToLog ¬
            with write permission)
        log "firstRunning = " & firstRunning
        set theSize to (get eof fileReference)
        if firstRunning = "" then
            set theSize to (get eof fileReference)
            log "theSize = " & theSize
            if theSize is equal to 0 then
                appendToFile(fileReference, "New log created on " & ((current date) as string) & " " & return)
                tell application "Finder"
                    set the creator type of the file pathToLog ¬
                        to "R*ch"
                end tell
            end if
            appendToFile(fileReference, "   --- debug on " & ((current date) as string) & "   --- " & return)
            tell current application
                set appPath to path to current application
            end tell
            appendToFile(fileReference, "   --- current AppleScript is " & appPath & " ---" & return)
            
            set firstRunning to "running"
        end if
        
        
        appendToFile(fileReference, theMessage & return)
        
        close access fileReference
        
    on error mes
        try
            log "  We got an error when writing to  " & mes
            close access fileReference
        end try
    end try
    
end debug


-- ----------------------------------------------------------  
-- http://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
-- Philip Regan, based on FileExists

on folderExists(theFile) -- (String) as Boolean
    tell application "System Events"
        -- for some reason this failed when the folder didn't exit.  hacked a solution.
        try
            if exists folder theFile then
                return true
            else
                return false
            end if
        on error errStr number errorNumber
            return false
        end try
    end tell
end folderExists

Aug 31, 2015 6:35 AM in response to rccharles

Hello rccharles,


By source code of script, I mean text version of script, which is plain text file with name extension applescript. I always save original script as text and save copy as compiled script or applet if need be, for I do not trust Script Editor's decompilation. 😝 Indeed Script Editor cannot resolve code to terminology unless relevant terminology dictionary is accessible in run-time environment, in which case the decompilation gives "incomplete" source code with embeded raw Apple Event codes.


In the current issue under discussion, the incompatible beast which leads to SIGSEGV under OS X 10.7 or earlier is the applet shell executable at:


Applet.app/Contents/MacOS/applet



which will load and execute the compiled script at:


applet.app/Contents/Resources/Scripts/main.scpt



which is mere data file. As long as the script uses only language features available in target OS, it should have been fine but appears not so due to the incompatibility in applet shell.


Regards,

H

Aug 31, 2015 6:57 PM in response to rccharles

This is amazing. Thank you so much for writing all that for me! I can't thank you enough. It is so beyond my scope to really understand what it's true value it, but I'll be reading through it in detail as I try and get this working on another machine properly.


Thank you! For some reason it won't let me say that these answers are helpful! But they are! Thank you again!!

Shared Applescript App Quits Unexpectedly

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.