Author Topic: Creating a new folder within the folder of a dropped file  (Read 1134 times)

0 Members and 1 Guest are viewing this topic.

Offline mbornbach

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Creating a new folder within the folder of a dropped file
« on: May 26, 2015, 03:24:30 PM »
I'm dropping Quark files onto an applescript droplet that opens them and makes pdf files. Right now, the pdf files are just loose in the same folder as the Quark files. I would like to create a folder named "PDF Files" within the parent folder that the dropped files are in. I'm stuck on targeting the parent folder that the dropped files are in, so that I can get the finder to create a new folder within that parent folder.  I plan on trying a couple other things once I figure out how to target the parent folder. Here's a sample of what I have so far:

on open droppedfile
   tell application "Finder"
      activate
      set this_folder to the POSIX path of (the container of droppedfile as alias)
      make new folder at this_folder with properties {name:"PDF Files"}
   end tell
   
   tell application "QuarkXPress" to activate
   repeat with afile in droppedfile
      tell application "QuarkXPress"
         open afile
         set my_destination to file path of document 1 as string
         set doc_name to get (characters 1 thru -5) of my_destination as string
         export document 1 in (doc_name & ".pdf") as "PDF" page range "1, 2" PDF output style "Quad PDF"
         close document 1 saving no
      end tell
   end repeat
end open

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: Creating a new folder within the folder of a dropped file
« Reply #1 on: May 27, 2015, 03:56:49 AM »
Hi! Where do you want the new folder to be created in? According to the source folder where the dropped files come from? Are you sure the container you want to get is that one?
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =

Offline mbornbach

  • Newbie
  • *
  • Posts: 34
  • Karma: 0
Re: Creating a new folder within the folder of a dropped file
« Reply #2 on: May 27, 2015, 09:00:19 AM »
Hi :) Yes. I want the new folder to be created inside the source folder where the dropped files come from.

Offline Jean-Marie Schwartz

  • Sr. Member
  • ****
  • Posts: 436
  • Karma: 6
  • Gender: Male
Re: Creating a new folder within the folder of a dropped file
« Reply #3 on: May 28, 2015, 04:31:08 AM »
Are all the files coming from a single source? (Hum, I don't even know if it's possible to drop files from several sources actually, but one never knows).
Well, this said sth like this might help you at least to see what you get:
Code: [Select]
on open droppedfile
set the_list to {}
repeat with afile in droppedfile
set end of the_list to afile -- list of aliases
end repeat
set the_choice to (choose from list the_list)
end open
Next step is to separate folder path from file name. Sth like this might be helpful too:
Code: [Select]
set the_folder to choose folder with prompt "PLEASE CHOOSE A FOLDER."
set the_file to choose file with prompt "PLEASE CHOOSE A FILE."

set parent_folder_of_the_file to (do shell script "dirname " & (quoted form of POSIX path of the_file))
set name_of_the_file to (do shell script "basename " & (quoted form of POSIX path of the_file))

beep 1
display dialog ("CHOSEN FOLDER:" & return & the_folder as string) & return & return & "CHOSEN FILE CONTAINER:" & return & parent_folder_of_the_file & return & "CHOSEN FILE NAME:" & return & name_of_the_file

-- CREATING A FOLDER VIA SHELL SCRIPT
-- do shell script "mkdir -p " & (quoted form of POSIX path of the_folder) & "PDFfiles" -- SO HERE THE_FOLDER IS THE FOLDER YOU CHOSE FIRST
Jean-Marie Schwartz
May the Schwartz be with you :-)

= Mac Pro Quad-Core Intel 2.8 GHz 10 Go 1066 MHz DDR3 Mac OS 10.8.5 QuarkXPress 9.5.4.1 Adobe CS5 =

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: Creating a new folder within the folder of a dropped file
« Reply #4 on: May 29, 2015, 01:34:26 PM »
try this:

Code: [Select]
on open of theFiles
-- Executed when files are dropped on the script
repeat with thisFile in theFiles
set parentFolder to my getParentFolder(thisFile)
if not my CheckForFolder(parentFolder & "PDF Files:") then
tell application "Finder"
make new folder at folder parentFolder with properties {name:"PDF Files"}
end tell
end if
-- Do something with thisFile
end repeat
end open

on getParentFolder(filePath)
set folderParts to my GetTextItem(filePath as text, ":", {1, -2})
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set parentFolder to folderParts as text
set AppleScript's text item delimiters to OldDelims

return (parentFolder & ":") as text
end getParentFolder

on CheckForFolder(thisFolder)
tell application "Finder"
return (exists folder thisFolder)
end tell
end CheckForFolder

on GetTextItem(ThisString, ThisDelim, ThisItem)
-- ThisString -> String to look in
-- ThisDelim -> Text element that delimit the string
-- ThisItem -> Number of the element to return
copy the text item delimiters to OldDelims
set the text item delimiters to ThisDelim
set arrItem to every text item of ThisString


if class of ThisItem is list then
set FromItem to item 1 of ThisItem
set ToItem to item 2 of ThisItem
set TheReturn to (items FromItem thru ToItem of arrItem) as text
else
if ThisItem ≠ 0 then
set TheReturn to (item ThisItem of arrItem) as text
else
set TheReturn to arrItem -- return every items
end if
end if
set the text item delimiters to OldDelims
return TheReturn
end GetTextItem
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5