Tuesday, January 23, 2018

#1 2017-12-29 02:35:21 pm

aaronspe
Member
Registered: 2016-04-19
Posts: 1

Getting a valid path back from terminal find command.

Hello,
  Basically I'm trying to  put up a dialog that uses the terminal find command to find a file, if more than one item with the same name is found it will display a choose from list dialog, and then delete the file and its parent directory. As usual, I don't quite know enough to actually make the the thing work. roll

Anyway, here's the script I'm hacking together, the directory being searched is a Windows Server. I noticed when it returns results in the choose file dialog it adds an extra / to path (like, parent//child/child) which is weird. I don't know what's going on there yet.

I know this is messy...

Any help appreciated,
Aaron

Applescript:



property kDir : ""

if kDir = "" then
   set kDir to choose folder
end if

set p to POSIX path of kDir
log p

set theResponse to display dialog "Which File to Delete?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

set kFileName to text returned of theResponse

set kPath to "find " & quoted form of p & " -name " & kFileName
log kPath

set killFiles to do shell script kPath


set myChoice to killFiles
choose from list KillFiles

--need to rm the parent directory of the chosen item(s)


Offline

 

#2 2017-12-29 03:33:19 pm

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

Re: Getting a valid path back from terminal find command.

Hi. Welcome to MacScripter.

The extra slash happens because find inserts a slash between the root path it's given and each subpath it finds. Since the root path here is a POSIX path derived from a folder alias, it already ends with a slash, so two slashes appear together in the results. But they behave as if they were one and nothing needs to be done about them.

The result from the shell script is a single text with one found path on each line, so you need a list of the text's paragraphs for choose from list. You should probably check that something's actually been found before invoking choose from list. If you think you may need to choose more than one of the items in the list for deleting, you'll need to use choose from list's multiple selections allowed parameter.

Applescript:



property kDir : ""

if kDir = "" then
   set kDir to choose folder
end if

set p to POSIX path of kDir
log p

set theResponse to display dialog "Which File to Delete?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"

set kFileName to text returned of theResponse

set kPath to "find " & quoted form of p & " -type f -name " & quoted form of kFileName
log kPath

set killFiles to do shell script kPath

if (killFiles is "") then return -- or show an error message (file name not found)

set killFiles to paragraphs of killFiles
set myChoice to (choose from list killFiles with multiple selections allowed)
if (myChoice is false) then return -- 'choose from list' returns 'false' if its Cancel button's used.

repeat with thisItem in itemsToKill
   
   --need to rm the parent directory of the chosen item(s)
   -- (Strip the file name from each chosen path and rm the result)
end repeat


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)